I have a class like
public class Document
{
[Key]
public int Id { get; set; }
public User ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
}
Here “User” is a class with some properties (all are primitive types ) Here is my mapping of class Document
HasRequired(a => a.ModifiedBy).WithMany().Map(b => b.MapKey("ModifiedBy")).WillCascadeOnDelete(false)
;
So in table "Document" ModifiedBy is a foreign key.
I am trying to update the ModifiedBy field of a document having id 10
Document document= new Document();
using (DBContext ctx = new DBContex()) {
User loggedinUser =ctx.Users.Where(u=>u.LoginId==loggedInUserId).FirstOrDefault() as User;
document.id=10;
document.ModifiedBy = loggedinUser;
document.ModifiedDate = DateTime.Now;
ctx.Entry(document).State = EntityState.Modified;
ctx.SaveChanges();
}
But the ModifiedBy field is not updating. only ModifiedDate field is updating.
Note that I don't what to update User table. I just want to update Document table