仅具有主键的带有实体框架的旧数据库的映射表关系 [英] Map tables relationship from legacy database w/ entity framework with only primary keys
问题描述
数据(表名)
dataid PK,
value1,
value2,
value3
data_address(表名)
dataaddressid PK,
dataid-errenddataid的ID,
addressid1-标识为id的地址,
addressid2-标识为id的地址,
类型
地址(表命名)
地址ID PK-地址ID1或地址ID2的ID,
地址1,
地址2,
名称,
邮政编码,
城市
我很难尝试使用Entity Framework 5映射这种关系,如果有人有想法或良好的链接,我将不胜感激!
如果确定数据库的完整性是正确的,则可以映射表并手动创建关联
在数据库优先模式下,我摆弄了一个简单的数据模型:没有FK的Parent + Child。这些表当然是导入的,它们之间没有关联。然后,我对父对象执行添加关联...,就像这样:
注意:尚无外键属性。我在关联属性中手动添加了它:
我可以在 Parent.Children
上运行linq查询。
我认为这是最简单的方法。 edmx设计图为您提供一些指导,以查看您创建了哪些关联。您始终可以添加代码生成项以生成 DbContext
,它比默认的 ObjectContext
易于使用。 / p>
data (table name)
dataid PK,
value1,
value2,
value3
data_address (table name)
dataaddressid PK,
dataid - id to errenddataid,
addressid1 - id to en addressid,
addressid2 - id to en addressid,
type
address (table namne)
addressid PK - id to addressid1 or addressid2,
address1,
address2,
name,
zipcode,
city
I have a really hard time trying to map this relations using Entity Framework 5, if some one have an idea or good links I would much appreciate that!
If you are certain that the database's integrity is sound you could just map the tables and create the associations manually in the EF model.
In a database-first mode I fiddled a bit with a simple data model: Parent + Child without FK. The tables were of course imported without association between them. Then I did "Add Association..." on the Parent, like so:
Note: no foreign key property yet. I added it manually in the properties of the association:
And I could run a linq query on Parent.Children
.
I think this is the easiest way for you. The edmx design surface gives you some guidance to see which associations you created. You can always add a code generation item to generate a DbContext
which is easiser to work with than the default ObjectContext
.
这篇关于仅具有主键的带有实体框架的旧数据库的映射表关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!