实体框架4.1 - 默认EntityState的FK? [英] Entity Framework 4.1 - default EntityState for a FK?

查看:90
本文介绍了实体框架4.1 - 默认EntityState的FK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,ASP.NET MVC和Entity Framework 4,我有一个名为UF,另外一个叫国家报的实体,他们有这样的关系:

  UF [* ... 0..1]派斯

我可以直接从UF使用导航属性访问派斯对象:

  UF.Pais.Whatever =foobar的;

目前我有一个观点这将插入一个新的项目到数据库中,并且它具有Pais.Codigo编辑(Codigo是派斯的主键)。所以,当我插入一个新的UF,框架创建UF类的实例,类派斯的一个实例的引用。那么这样做是:

 如果(ModelState.IsValid)
{
    db.UFs.AddObject(UF);
    db.SaveChanges();    返回查看();
}

的问题是,在EF被插入一个新派斯到数据库中,因此,它基本上忽略了现有

例如,如果让我们说我对象用友拥有一个派斯具有1的ID uf.Pais.Codigo的当前值是1,其他的属性,如描述,目前为空。当我执行的SaveChanges,无论是UF和uf.Pais是添加了的状态。对于uf.Pais正确的状态应该是不变的,因为它已经在数据库中存在。

我的问题是:有改变不改变默认的关系EntityState的一些方法?下面code解决了问题,但将它添加到每个功能与增加了一个新的条目添加到数据库(和每个FK)是矫枉过正!

  db.ObjectStateManager.ChangeObjectState(uf.Pais,EntityState.Unchanged);

就是这样。我不知道如果我是不够清楚。随意如果需要问的更多信​​息。很遗憾任何英文错误!

谢谢,

里卡多

PS:国家报表示国家与UF的国家


解决方案

  

我的问题是:有更改默认关系的一些方法
   EntityState 不变


通过调用连接,而不是不变


  

以下code解决了问题,但它添加到每个功能
  用添加了一个新条目的数据库(和每个FK)是矫枉过正!


没有它不是矫枉过正,这是一个解决方案,因为无论是连接 ADDOBJECT 总会使操作在实体图中所有的实体和协会。这意味着,调用 ADDOBJECT 将让一切什么情况下是不知道的还没有为添加连接将让一切什么情况下是不知道的为不变(这样你将依次有每个修改或插入的实体设置为正确的状态)。这是EF如何工作的,如果你使用的是分离的实体。

有关问题的另一个解决方案是使后连接 UF 添加

  //这里UF.Pais为null
db.UFs.AddObject(UF);
//创建虚拟派斯
VAR派斯=新派斯{ID =Codigo};
//使上下文感知派斯
db.Pais.Attach(派斯);
//现在做的关系
uf.Pais =派斯;
db.SaveChanges();

如果您正在使用分离的实体工作,你总是负责制定<一个href=\"http://stackoverflow.com/questions/3635071/update-relationships-when-saving-changes-of-ef4-poco-objects/3635326#3635326\">correct国家为每个实体(和<一个href=\"http://stackoverflow.com/questions/5281974/$c$c-first-independent-associations-vs-foreign-key-associations/5282275#5282275\">independent协会)。所以,你要么使用连接实体,让EF让你的魔力(如例所示),或者你会用你喜欢的方式。在更复杂的方案中,可以发现,最好的办法是引入的变更再次加载实体图和合并到连接图。

I'm having a small problem with ASP.NET MVC and Entity Framework 4. I have an entity called "UF" and another one called "Pais", and they have this relation:

UF [* ... 0..1] Pais

I can access the Pais object directly from UF using a navigation property:

UF.Pais.Whatever = "foobar";

Currently I have a View which inserts a new item into the database, and it has an editor for "Pais.Codigo" ("Codigo" is the primary key for Pais). So when I insert a new UF, the framework creates an instance of class UF with a reference to an instance of class Pais. Then this is done:

if (ModelState.IsValid)
{
    db.UFs.AddObject(uf);
    db.SaveChanges();

    return View();
}

The problem is that the EF is inserting a new Pais into the database, so it basically ignores the existing one.

For example, if let's say my object UF has a Pais with an ID of 1. The current value of uf.Pais.Codigo is 1. Other attributes, like the description, is currently null. When I execute the SaveChanges, both "uf" and "uf.Pais" are with the state of Added. The correct state for "uf.Pais" should be Unchanged, since it already exists on the database.

My question is: there's some way of changing the default relationship EntityState for Unchanged? The following code solves the problem, but adding it to each function with adds a new entry to the database (and for each FK) is overkill!

db.ObjectStateManager.ChangeObjectState(uf.Pais, EntityState.Unchanged);

That's it. I'm not sure if I was clear enough. Feel free to ask more information if needed. And sorry for any english mistakes!

Thanks,

Ricardo

PS: "Pais" stands for Country and "UF" for State.

解决方案

My question is: there's some way of changing the default relationship EntityState for Unchanged?

Yes by calling Attach instead of Unchanged.

The following code solves the problem, but adding it to each function with adds a new entry to the database (and for each FK) is overkill!

No it is not overkill, it is a solution because either Attach or AddObject will always make the operation for all entities and associations in entity graph. That means that calling AddObject will make everything what context is not aware of yet as Added and Attach will make everything what context is not aware of as Unchanged (so you will in turn have to set each modified or inserted entity to its correct state). That is how EF works if you are using detached entities.

Another solution for the problem is making the connection after the UF is added:

// Here UF.Pais is null
db.UFs.AddObject(uf);
// Create dummy Pais
var pais = new Pais { Id = "Codigo" };
// Make context aware of Pais
db.Pais.Attach(pais);
// Now make the relation
uf.Pais = pais;
db.SaveChanges();

If you are working with detached entities you are always responsible for setting the correct state for each entity (and independent association). So you will either use attached entities to let EF make the magic for you (as shown in the example) or you will use the approach you dislike. In more complex scenarios you can find out that best approach is to load entity graph again and merge incoming changes into the attached graph.

这篇关于实体框架4.1 - 默认EntityState的FK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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