Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

c# - Updating a reference to a child object in Entity framework 4.1 (CodeFirst)

I'm trying to update an object that I have previously saved with EntityFramework 4.1 (CodeFirst)

The class Job has the following properties ...

public class Job
{
    [key]
    public int Id { get; set; }
    public string Title { get; set; }
    public Project Project { get; set; }
    public JobType JobType { get; set; }
    public string Description { get; set; }
}

The initial create works fine, but the update only commits changes to the strings..

If I change the child objects eg the JobType Property from JobTypeA to JobTypeB - the change is not committed ...

I'm not looking to commit a change to JobType - only to Job.

using (var context = new JobContext())
{
    context.Jobs.Attach(job);
    context.Entry(job).State = EntityState.Modified;
    context.SaveChanges();
}

Having a look at SQL Profiler - the Ids are not even being sent for the Update - however they are for the initial insert!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Setting the state to Modified only updates scalar and complex properties, not your navigation properties. This only goes through Entity Framework's change detection. It means that you need to load the original from the database:

using (var context = new JobContext())
{
    var originalJob = context.Jobs.Include(j => j.JobType)
        .Single(j => j.Id == job.Id);

    // Update scalar/complex properties
    context.Entry(originalJob).CurrentValues.SetValues(job);

    // Update reference
    originalJob.JobType = job.JobType;

    context.SaveChanges();
}

You could probably also leverage some "tricks" in your case:

using (var context = new JobContext())
{
    var jobType = job.JobType;
    job.JobType = null;

    context.JobTypes.Attach(jobType);
    context.Jobs.Attach(job);
    // change detection starts from here,
    // EF "thinks" now, original is JobType==null

    job.JobType = jobType;
    // change detection will recognize this as a change
    // and send an UPDATE to the DB

    context.Entry(job).State = EntityState.Modified; // for scalar/complex props

    context.SaveChanges();
}

It wouldn't work though if you want to set JobType to null.

This is a typical situation which is getting much simpler if you expose foreign keys as properties in your model: With a JobTypeId in your Job entity your code would work because the FK property is scalar and setting the state to Modified will also mark this property as modified.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...