简体   繁体   English

如何使用SQL映射具有列表属性的实体框架模型

[英]How to map an Entity Framework model that has a list property using SQL

I am using Database first approach to map my tables to entity models. 我正在使用数据库优先方法将表映射到实体模型。 Currently I have two tables, Orders and Products: 目前我有两个表,订单和产品:

Orders: Id, DateCreated

Products: Id, Name, OrderId

As you can see it's a one to many relationship. 如您所见,这是一对多的关系。 And I would like to create a view using these two tables and map it to an entity called Order and Products : 我想使用这两个表创建一个视图并将其映射到名为OrderProducts的实体:

public class Order
{
    public int Id {get;set;}
    public DateTime DateCreated {get;set;}
    public IList<Product> Products {get;set;}
}

public class Product
{
    public int Id {get;set;}
    public string Name {get;set;}
}

I know how to map a single property, but have no idea how I would approach it for a list property. 我知道如何映射单个属性,但不知道如何为列表属性处理它。 Any ideas? 有任何想法吗?

Please note that the tables are slightly more complicated and I HAVE TO use views to map these entities. 请注意,表稍微复杂一些,我必须使用视图来映射这些实体。

there are a lot of guides on it around the web. 网上有很多指南。 one of them is @ msdn site 其中之一是@ msdn网站

Replace IList with ICollection and you should be good with that. 用ICollection替换IList,您应该会满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM