andrelange91
andrelange91

Reputation: 1188

Read only when updating db records

"Property or indexer cannot be assigned to -- it is read only"

i see a lot of similar question though none actually seem to help me.

i am updating some rows in my db.

first i call my data:

var projects = (from p in _ProjectRep.GetAll()
                        join cl in _ClientRepo.GetAll() on p.ClientID equals cl.ClientID
                        where p.IsOpen == true && p.Tasks.Any(t => t.TimeEntries.Any(te => te.DateEntity >= dateLimit)) == false && p.Tasks.Any(te => te.TimeEntries.Any())
                        select new {
                            TickProjectID = p.TickProjectID,
                            ClientName = cl.ClientName,
                            ProjectName = p.ProjectName,
                            IsOpen = p.IsOpen,
                            DateClosed = p.DateClosed
                        }).ToList();

Then i try to loop through that data, and update specific fields in those records.

foreach (var item in projects)
        {// Update projects to closed.
            item.IsOpen = false;
            item.DateClosed = DateTime.Now;
        }

and lastly save the data..

// updates changes to OUR db.
   var affectedCount = _UnitOfWork.SaveChanges(); 

But i keep getting that error, and i do not know what to do about it. What i can read it is something about get/set, but i have none of those, and i cannot see why those should be nessesary ?

Hoping someone can give a clear solution, that might help me.

Edit

private readonly ProjectRepository _ProjectRep;

_ProjectRep = new ProjectRepository(unitOfWork);

Upvotes: 0

Views: 54

Answers (2)

Tomas Chabada
Tomas Chabada

Reputation: 3019

var projects = (from p in _ProjectRep.GetAll()
                    join cl in _ClientRepo.GetAll() on p.ClientID equals cl.ClientID
                    where p.IsOpen == true && p.Tasks.Any(t => t.TimeEntries.Any(te => te.DateEntity >= dateLimit)) == false && p.Tasks.Any(te => te.TimeEntries.Any())
                    select p).ToList();

Try this. Problem in your code is, that you are working with anonymous type (created using new {...}). This type doesn't know anything like change tracking, its properties are read only, so this type can not be used to save changes to database.

Upvotes: 1

andrelange91
andrelange91

Reputation: 1188

it seems my sql call was anonymous, and therefore it did not know what it was.

i changed my sql call select

 var projects = (from p in _ProjectRep.GetAll()
                        join cl in _ClientRepo.GetAll() on p.ClientID equals cl.ClientID
                        where p.IsOpen == true && p.Tasks.Any(t => t.TimeEntries.Any(te => te.DateEntity >= dateLimit)) == false && p.Tasks.Any(te => te.TimeEntries.Any())
                        select new {
                            Project = p,
                            ClientName = cl.ClientName

                        }).ToList();

and then i just have to call "Project.{something}" when i wanted to select data.

like this:

foreach (var item in projects)
        {// Update projects to closed.
            item.Project.IsOpen = false;
            item.Project.DateClosed = DateTime.Now;
        }

that made the whole thing work.

Upvotes: 0

Related Questions