层级上的实体 [英] Entities over tiers

查看:68
本文介绍了层级上的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否是对VS2008中包装的EF的v1的限制与否,但我想在我走更长的路线之前先询问专家。

I'm not sure if this is a limitation or not with v1 of the EF packaged in VS2008 or not but I want to ask the experts first before I go longer routes.


推荐答案

Hello Synced23,

虽然你没有包含实际的异常消息你知道,我猜你在调用AddObject或Attach / AttachTo时可能会看到一些冲突。一些澄清可能会有所帮助:

1。 Attach / AttachTo和AddObject(在生成的方法AddToNodes中内部调用)假设您要添加或附加连接到您传递的实体的整个图形作为参数。对于示例代码,这意味着EF将尝试添加您传递给数据库的nodeType。如果nodeType碰巧实际上是现有的NodeType,并且您只想表示新节点和现有nodeType之间存在关系,那么有几种方法可以反映出来,最简单的可能就是这样:
public void Add(Node node,NodeType nodeType)
{
model.AddToNodes(node);
model.AttachTo(" NodeTypes",nodeType);

node.NodeType = nodeType;
model.SaveChanges();


其他替代方法可以使用AcceptChanges将nodeType标记为不变:

公共void Add(节点节点)
{
model.AddToNodes(node);
var entry = model.ObjectStateManager.GetObjectStateEntry(node.NodeType);
entry.AcceptChanges();
model.SaveChanges();


或者简单地在NodeTypeReference属性上使用nodeType的键值设置EntityKey,而不是实际设置参考:

public void Add(Node node,NodeType nodeType)
{
model.AddToNodes(node);
node.NodeTypeReference.EntityKey = nodeType.EntityKey;
model.SaveChanges();
}


2。具有短期上下文通常是好的,但有时将ObjectContext的生命周期与单个请求的处理对齐更为实际。从这个意义上讲,如果DAL.NodeTypes.GetByName("pages")实际上是在查询数据库并在状态管理器中获取nodeType,那么使用相同的上下文来添加节点将有助于简化您的工作。这是因为上下文中的状态管理器将记住nodeType不是新的NodeType,而是已存在于数据库中的NodeType。然后,当您将节点连接到nodeType时,状态管理器将获知该节点是与该现有nodeType关联的新对象。那时,如果你使用的是代码类,那么model.AddToNodes(node)实际上是一个no-op(状态管理器已经知道了节点,因为它在分配节点时收到了一个通知.NodeType = nodeType发生了。)
希望这有帮助,
迭香
Hello Synced23,

Although you haven't included the actual exception messages that you get, I can guess that you might be seeing some conflicts when invoking AddObject or Attach/AttachTo. A few clarifications might help:

1. Attach/AttachTo and AddObject (which is internally invoked in the generated method AddToNodes) assume you want to either add or attach the entire graph connected to the entity you pass as an argument. For your sample code, this means that EF will try to add the nodeType you pass to the database. If nodeType happens to actually be an existing NodeType, and you only want to represent that there is a relationships between the new node and an existing nodeType, there are several ways to reflect that, the simplest probably being something like this:

public void Add(Node node, NodeType nodeType)
{
    model.AddToNodes(node);
    model.AttachTo("NodeTypes",nodeType);
    node.NodeType = nodeType;
    model.SaveChanges();
}

Other alternative can be using AcceptChanges to mark nodeType as unchanged:

public void Add(Node node)
{
    model.AddToNodes(node);
    var entry = model.ObjectStateManager.GetObjectStateEntry(node.NodeType);
    entry.AcceptChanges();
    model.SaveChanges();
}

Or simply set the EntityKey on the NodeTypeReference property with the key values of nodeType instead of actually setting the refererence:

public void Add(Node node, NodeType nodeType)
{
    model.AddToNodes(node);
    node.NodeTypeReference.EntityKey = nodeType.EntityKey;
    model.SaveChanges();
}


2. Having short lived context is in general good, but it is sometimes more practical to align the lifetime of an ObjectContext to the processing of a single request. In that sense, if DAL.NodeTypes.GetByName("pages") is actually querying the database and getting nodeType in the state manager, then using the same context to add the node would have helped simplify things for you. This is because the state manager in the context will remember that nodeType is not a new NodeType but one that already exists in the database. Then when you connect the node to nodeType, the state manager learns that node is a new object associated to that existing nodeType. At that point, if you were using code-gen classes, then model.AddToNodes(node) would actually be a no-op (the state manager already knows about node, because it received a notification at the moment the assigment node.NodeType = nodeType happened).

Hope this helps,
Diego


这篇关于层级上的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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