0

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

Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95
  • You want to get document from the database by that particular ID. `ctx.Users.Where(u=>u.LoginId==loggedInUserId).FirstOrDefault() as User` can be reduced to `ctx.Users.FirstOrDefault(u => u.LoginId == loggedInUserId)` and also you need to think whether to use `FirstOrDefault` or `SingleOrDefault`. – Callum Linington Jun 15 '15 at 13:45
  • Thanks but that is not an answer – Kuttan Sujith Jun 15 '15 at 13:48
  • That is why it is in the comments..... and p.s. it also is an answer, the top line is, just not extremely explicit – Callum Linington Jun 15 '15 at 13:49

2 Answers2

0

This is what I would do in your situation:

using (var ctx = new DBContext())   {

    var loggedinUser = ctx.Users.SingleOrDefault(u=>u.LoginId==loggedInUserId);

    if (loggedinUser == null) throw new Exception("Can't find logged in user");

    var document = ctx.Documents.SingleOrDefault(x => x.id == 10);

    if (document == null) throw new Exception("Can't find associated document");

    document.ModifiedBy = loggedinUser;
    document.ModifiedDate = DateTime.Now;

    ctx.SaveChanges();   
}

Now the reason I do this.

When I do var document = ctx.Documents.SingleOrDefault(x => x.id == 10); I'm calling that particular entity into EF's change tracker. Which means if I change any of its properties such as ModifiedDate EF will automatically do ctx.Entry(document).State = EntityState.Modified;.

If you don't do this, and you want to carry on the way you do it, then you need to add this line (I believe):

document.ModifiedDate = DateTime.Now;
ctx.Documents.Attach(document); 
ctx.Entry(document).State = EntityState.Modified;
ctx.SaveChanges();  
Callum Linington
  • 14,213
  • 12
  • 75
  • 154
  • This a sample only I don't have just 2 properties.I have around 15 properties. So doing an update as you do wont be good for me – Kuttan Sujith Jun 15 '15 at 13:52
  • Well, I have answered your question above. The case that you have presented in your comments isn't really a Stackoverflow question. It is more for Meta Stackoverflow. – Callum Linington Jun 15 '15 at 13:53
  • Thanks Callum,But ctx.Documents.Attach(document); ctx.Entry(document).State = EntityState.Modified; not helping me – Kuttan Sujith Jun 15 '15 at 14:41
0

Base on Entity Framework does not update Foreign Key object I created

public string ModifiedBy { get; set; }

[DataMember]
[ForeignKey("ModifiedBy")] 
public User ModifiedUser
        {
            get;
            set;
        }

That means I additionally introduced a primitive type(string ModifiedBy ) for Modified User and specified ForeignKey. That resolved my issue

Community
  • 1
  • 1
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95