I have a model that is used to modify a user's password and captures the date that the password has been changed. I am getting an error saying property or indexer anonymous type cannot be assigned to it is read only I get that error for both fields the password and CreateDate . I only know of 1 way to select multiple fields from entity and this is it..

  var old= db.registration.Where(b => b.email == getid.email).Select
(s => new { s.password, s.CreateDate }).FirstOrDefault();

                     old.CreateDate = DateTime.Now();

                     old.password = new.password;
                     db.Entry(old).State = EntityState.Modified;
                     db.SaveChanges();

Is there a way that I can assign these new values without getting the error message or a workaround ?

有帮助吗?

解决方案

Just select the default object and run with it

var old= db.registration.Where(b => b.email == getid.email).FirstOrDefault();
old.CreateDate = DateTime.Now();
old.password = new.password;
db.Entry(old).State = EntityState.Modified;
db.SaveChanges();

The error message you're getting is likely because you're assigning to the index of an anonymous type, ie.

 s => new { s.password, s.CreateDate }

is anonymous and its indexed properties cant be assigned to

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top