实体框架code首先 - 为什么我不能这样更新复杂属性? [英] Entity Framework Code First - Why can't I update complex properties this way?

查看:119
本文介绍了实体框架code首先 - 为什么我不能这样更新复杂属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小样本项目中使用实体框架4.1(code第一)工作。我的课是这样的:

I'm working on a small sample project using Entity Framework 4.1 (code first). My classes look like this:

public class Context : DbContext
{
    public IDbSet<Person> People { get; set; }
    public IDbSet<EmployeeType> EmployeeTypes { get; set; }
}

public class Person
{
    [Key]
    public int Key { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    virtual public EmployeeType EmployeeType { get; set; }
}

public class EmployeeType
{
    [Key]
    public int Key { get; set; }
    public string Text { get; set; }

    virtual public ICollection<Person> People { get; set; }
}

我已经保存了几个EmployeeTypes(第一,第二)到数据库中,我已经保存了一个人谁拥有第一类。现在,我想修改的人。我知道我可以通过加载的人,更改属性,然后保存做到这一点。但我想做的事情,而不是,这似乎对我来说,它应该工作,是这样的:

I've saved a couple EmployeeTypes ("first", "second") to the database, and I've saved a Person who has the first type. Now I want to modify the Person. I know I can do this by loading the Person, changing properties, and then saving. But what I want to do instead, which seems to me like it ought to work, is this:

var c = new Context();
var e = c.EmployeeTypes.Single(x => x.Text.Equals("second"));
var p = new Person { 
            Key = originalKey,       // same key
            FirstName = "NewFirst",  // new first name
            LastName = "NewLast",    // new last name
            EmployeeType = e };      // new employee type
c.Entry(p).State = EntityState.Modified;
c.SaveChanges();

奇怪的是,这改变名字和姓氏但不EmployeeType。如果我得到一个新的上下文,并要求此人时,EmployeeType保持设置为第一次,因为它是这个code跑了。

Oddly, this changes FirstName and LastName but not EmployeeType. If I get a new Context and request this Person, the EmployeeType remains set to "first" as it was before this code ran.

什么我需要做的就是导航性能进行更新,而不仅仅是标属性?(这尤其是因为百思不得其解的EmployeeType,实际上需要改变的唯一的事情就是外键在Person表,该键是一个标量属性。)

What do I need to do to get the navigation properties to update, and not just the scalar properties? (This is especially puzzling because for EmployeeType, the only thing that actually needs to change is the foreign key in the Person table, and that key is a scalar property.)

(顺便说一句,我知道我可以先检索的人,再一个接一个不断变化的性能做到这一点,但我使用模型在ASP.NET MVC结合,好像这样会更容易,因为我有没有更新的人,对象已经在我的POST方法。)

(By the way, I know I can do this by retrieving the Person first, then changing properties one-by-one, but as I'm using model binding in ASP.NET MVC, it seems like this way would be easier because I'll have the updated person object already in my POST method.)

推荐答案

您可以尝试不同的方式:

You can try it different way:

var c = new Context();
var e = c.EmployeeTypes.Single(x => x.Text.Equals("second"));
var p = new Person { 
            Key = originalKey,       // same key
            FirstName = "NewFirst",  // new first name
            LastName = "NewLast"};   // new last name
c.People.Attach(p); // Attach person first so that changes are tracked 
c.Entry(p).Reference(e => e.EmployeeType).Load();               
p.EmployeeType = e; // Now context should know about the change
c.Entry(p).State = EntityState.Modified;
c.SaveChanges();

另一种方法是在暴露喜欢你的实体的外键:

public class Person
{
    [Key]
    public int Key { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [ForeignKey("EmployeeType")]
    public int EmployeeTypeKey { get; set; }
    public virtual EmployeeType EmployeeType { get; set; }
}

这将 EmployeeType 之间的独立相关变化的关系的类型外键关联。而不是指定导航财产分配外键属性。这将允许您通过您的当前code修改关系。

This will change the type of relation between Person and EmployeeType from Independent association to Foreign key association. Instead of assigning the navigation property assign the foreign key property. This will allow you to modify relation by your current code.

问题是,独立协会(那些不使用外键属性)在状态管理器/变更跟踪单独的对象进行处理。所以你的人修改并没有影响到现有关系的状态既不设置新的关系。 <一href=\"http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/192f6f64-df77-4902-901b-c36e2b157ab6\">I问MSDN上的如何使用API​​的DbContext做,但它可能只有当你施放的DbContext 的ObjectContext 并使用 ObjectStateManager ChangeRelationshipState

Problem is that independent associations (those don't using foreign key property) are handled as separate object in state manager / change tracker. So your modification of the person didn't affect state of the existing relation neither set the new relation. I asked on MSDN how to do it with DbContext API but it is possible only if you cast DbContext to ObjectContext and use ObjectStateManager and ChangeRelationshipState.

这篇关于实体框架code首先 - 为什么我不能这样更新复杂属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆