MS EF4:如何破译自引用表?
我有一个产品表,其中包含以下字段:
public class Product {
public int ID {get;set;}
...
}
以及一个将产品连接到其他“交叉销售”产品(如商品)的表:
public class CrossSell {
public int ProductID {get;set;}
public int CrossSellID {get;set;}
}
我的目的是,给定任何产品,获取交叉销售产品的列表。
Entity Framework 4(EF 第二版)采用这些表并创建 Product 和 Product1 关系,其中连接表的行称为 CrossSell。
我想要的是:
public class Product {
public int ID {get;set;}
...
public IList<Product> CrossSells {get;set;}
}
EF 创建了这个:
public class Product {
public int ID {get;set;}
...
public IList<Product> Products {get;set;}
public IList<Product> Products1 {get;set;}
}
我可以简单地删除名为 Products1 的第二个列表,将第一个列表重命名为“CrossSells”,然后这一切就可以神奇地工作吗?我是否应该以不同的方式重新创建 SQL 表,以便 EF 更好地理解我的意图?如果单个产品可以有多个交叉销售项目,这些 SQL 表会是什么样子?
编辑:
我真正寻找的是一种在 EF 中表示 CrossSell 项目而无需循环引用的方法。现在,产品指的是其他产品,而其他产品又指的是其他产品,而其他产品又...
I have a Product table with fields like:
public class Product {
public int ID {get;set;}
...
}
and a table that joins Products to other "cross sell" products (like items):
public class CrossSell {
public int ProductID {get;set;}
public int CrossSellID {get;set;}
}
My intent is, given any product, get a list of cross sell products.
Entity Framework 4 (EF second edition) takes these tables and creates a Product and Product1 relation, where the line that joins the table is called a CrossSell.
What I want to have is:
public class Product {
public int ID {get;set;}
...
public IList<Product> CrossSells {get;set;}
}
EF created this:
public class Product {
public int ID {get;set;}
...
public IList<Product> Products {get;set;}
public IList<Product> Products1 {get;set;}
}
Could I simply delete the the second List called Products1, rename the first to "CrossSells" and have all this just magically work? Should I remake the SQL tables differently so EF better understands my intent? What would these SQL tables look like if a single product can have multiple cross sell items?
EDIT:
What I am really looking for is a way to represent CrossSell items in EF without a circular reference. Right now, Product refers to other products which in turn refer to other products which in turn ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模型浏览器中找到实体。找到
Products1
属性。单击它。在属性编辑器中重命名它。Find the entity in Model Browser. Find the
Products1
property. Click it. Rename it in the property editor.