EF 4 Tree具有包含数据的连接 [英] EF 4 Tree with connections that have data

查看:60
本文介绍了EF 4 Tree具有包含数据的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从数据库中映射树结构,包括连接。


节点(PK id


连接(PK [FK ,FK 儿童], DataColumns );   - 多对多表


1.如果我从db生成模型,并且Connection表只有PK列模型可以,并且有node.Parents,node.Children导航。(例如,检测到很多许多并创建节点实体,跳过连接实体)


2.但是当其他列添加到连接表时,会生成一个连接实体(正常),但现在Node只有导航到Connection。如果我尝试手动将新的Association映射到模型中,我得到:


"具有可能不同键的两个实体被映射到同一行。确保这两个映射片段将AssociationSet的两端映射到相应的列。"


基本上我尝试使用与1中相同的模型关联。


<那么问题是如何映射这个结构来获取:node.ParentConnections,node.ChildConnections,node.ParentNodes,node.ChildNodes用于Node实体? (我知道我可以从节点导航到连接到节点或向Node实体添加属性以获取所有子节点/父节点,但我想知道是否可以通过映射完成)

解决方案

不确定是否有一个好的答案:您可以通过编写查询连接实例集的代码,在连接实体类型之上层集集合属性。例如。在你的部分类中编写代码。但是,对于这些属性,您将无法使用LINQ to Entities,但对于图形导航,这可能不是什么大问题。


否则,您可以编写一个显示连接对象为"纯FK / FK"。表和地图*:*关联到它。当然,它是只读的,但你可以使用Connection类型写入数据库...


我们之前被要求支持与有效载荷的关联,但第一个解决方案上面已经非常接近......


Hi,

I want to map a tree structure from db including connections.

Node (PK id )

Connection(PK [FK Parent ,FK Child ], DataColumns );  - many to many table

1.If I generate model from db and Connection table has only PK columns model is ok and has node.Parents, node.Children navigations.(ef detects many to many and creates Node entity, skipping Connection entity)

2.But when other columns are added to Connection table a Connection entity is generated(normal) but now Node only has navigations to Connection. If I try to manually map new Association into the model I get:

"Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns."

Basically I tried doing the same model association as in 1.

So the question is how can I map this structure to obtain: node.ParentConnections, node.ChildConnections, node.ParentNodes, node.ChildNodes for the Node entity? (I know I could navigate from node to connection to node or add a property to Node entity to get all child/parent Nodes but I wonder if it can be done from mapping)

解决方案

Not sure if there is a good answer: You can layer collection properties on top of a connection entity type by writing code that queries the set of connection instances. E.g. write code in your partial class. You won't be able to use LINQ to Entities against those properties though, but for graph navigations, that may not be a big deal.

Otherwise, you can write a view that presents the Connection object as a "pure FK/FK" table and map that *:* association to it. Will be read-only, of course, but you can write to the database using the Connection type...

We've been asked to support associations with payload before, but the first solution above is already pretty close to that...


这篇关于EF 4 Tree具有包含数据的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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