简体   繁体   English

如何使用实体框架将表映射到可枚举的属性? (数据库优先)

[英]How to map a table to an enumerable property using entity framework? (Database first)

Suppose I had these two classes: 假设我有以下两个类:

public class MyClass {
    public int Id { get; set; }
    public IEnumerable<MyData> MyDataCollection { get; set; }
}

public class MyData {
    public int DataId { get; set; }
    public string Data { get; set; }
    public int Id { get; set; }
}

and I had two tables in my database: 我的数据库中有两个表:

MyClasses:
Id
1
2


MyDatas:
DataId     Id       Data
1       -  1    -  "Hello"
2       -  1    -  "World"
3       -  2    -  "Hello World"

How do I use entity framework to link them up, so that I can do: 我如何使用实体框架将它们链接起来,以便可以执行以下操作:

using (var db = new DataContext()) {
     var data = db.MyClass.Where(c => c.ID == 1).MyDataCollection;
     foreach (var item in data) Console.WriteLine(item.Data);
}

I have the other data but so far I've just written [NotMapped] above the MyDataCollection property, but obviously I want to get rid of that and have it mapped. 我还有其他数据,但到目前为止,我只是在MyDataCollection属性上方编写了[NotMapped],但显然我想摆脱它并进行映射。 How do I (correctly) map it? 我如何(正确)映射它?

You can do it by expanding your class with appropriate navigation properties to establish the joins between them by following EF standards 您可以通过使用适当的导航属性扩展类以遵循EF标准在它们之间建立连接来做到这一点

public class MyClass {
    public MyClass()
    {
       MyDataCollection = new List<MyData>();
    }
    public int Id { get; set; }
    public virtual ICollection<MyData> MyDataCollection { get; set; }
}

public class MyData {
    public int DataId { get; set; }
    public string Data { get; set; }
    public int Id { get; set; }
    public virtual MyClass MyClass { get; set; }
}

Make sure these 2 entities are declared in the dbcontext as below 确保这两个实体在dbcontext中声明如下

public class MyContext: DbContext 
{
    public MyContext(): base()
    {

    }

    public DbSet<MyClass> MyClasses { get; set; }
    public DbSet<MyData> MyDatas { get; set; }            
}

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

相关问题 如何在Codefirst实体框架中显式将属性映射到数据库表列 - How to map a property to database table column explicitly in codefirst Entity framework 如何使用实体框架将属性映射到不同的表 - How to map a property to a different table using Entity Framework 如何使用数据库优先实体框架更改表名 - How to change table name using database first Entity Framework 首先使用代码后更改实体框架数据库架构映射 - Change Entity Framework database schema map after using code first 可以在Distinct属性上枚举过滤实体框架 - Filter Entity Framework enumerable on Distinct property 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first? 使用Entity Framework将模型映射到现有数据库表 - Map model to existing database table using Entity Framework 在MVC 5中未使用Entity Framework Database First填充AspNetUserRoles表 - AspNetUserRoles Table is not populated using Entity Framework Database First in MVC 5 使用实体框架时如何自动将所有 Enumerable 转换为 List? - How to automatically convert all Enumerable to List when using Entity Framework? 实体框架数据库优先属性验证 - Entity Framework Database First Property Validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM