Defining a 1 to many relationship in Entity Framework

I'm trying to create a 1:m relationship using Entity Framework (.net 4.0) and am getting the following error:

App_Code.Model.msl(36,6) : error 3007: Problem in mapping fragments
starting at lines 6, 36:Column(s) [ProductId] are being mapped in
both fragments to different conceptual side properties.

What i have is a Products table, and a Features table. The idea is that Products have many Features. Products each have a ProductId, and the Features have a ProductId foreign key.

Now the catch is that the foreign key doesn't exist in sql server, and i don't want it to. If it did, then it all automagically works nicely.

In the EDMX designer, i created an association from the product to the feature entity, then edited the mapping details of the ProductFeature association to be based on the Features table, which i think would make it work.

Any ideas? Thanks very much.


This is a M x N relationship. Why? Because a feature can be assigned to more than one type of product.

You should have a table ProductFeatures like so:

ProductId  FeatureId
1          1
1          2
2          1
2          2

Found one solution: delete the scalar property 'ProductId' from the feature entity:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/a71901fb-97ec-4072-949a-c0c66a9775b1

However, in the auto-generated relationships that EF gives you if you set up the foreign key in the database, the eg 'ParentId' fields are present in the child as a scalar field.

So i'm a little confused still.

-edit- Further help:

http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx

链接地址: http://www.djcxy.com/p/6390.html

上一篇: 最佳实践多语言网站

下一篇: 在实体框架中定义一对多关系