简体   繁体   English

首先使用EF代码,子级包含父级对象列表

[英]EF code first, child holds a list of parent objects

I have a nasty problem with EF code first where I need some help. 首先我在EF代码方面遇到了麻烦,需要帮助。 I have a Student which inherit from Person . 我有一个从Person继承的Student The Student can have friends like the Worker which inherit also from person. 学生可以有像Worker这样的朋友,也可以从人那里继承下来。

class Person
{
    public int Id { get; set; }
    //...
}

class Student : Person
{
    public virtual List<Person> Friends { get; set; }
    //...
}

class Worker : Person
{
    //there are other classes like this one
}

This is the exception I get if I try to write the entities to my sqlite DB: 如果尝试将实体写入sqlite DB,这是我得到的例外:

The member with identity 'PersonSelf' does not exist in the metadata collection. 元数据集合中不存在标识为“ PersonSelf”的成员。

If I change the list for example to Worker it is working but not with Person . 例如,如果我将列表更改为Worker ,则它正在工作,但与Person无关。 What can I change to solve this? 我该如何解决? Thank you for your help. 谢谢您的帮助。

You should not have any problem If you have defined your models and db context well I think. 您应该没有任何问题,如果我已经很好地定义了模型和数据库上下文,那么我认为。 I have created a small repo on github to test your models. 我在github上创建了一个小型仓库来测试您的模型。 Here is the link github repo My models are very very simple: 这是github repo的链接我的模型非常非常简单:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}
public class Student : Person
{
    public Student()
    {
        Friends=new List<Person>();
    }
    public virtual List<Person> Friends { get; set; }
    public int StudentAge { get; set; }
}
public class Worker : Person
{
    public decimal WorkerSalary { get; set; }
}

I'm using DotNet Core and I was able to create database and insert data. 我正在使用DotNet Core,并且能够创建数据库和插入数据。

Please check the repo and let me know If it works for you. 请检查存储库,让我知道它是否适合您。

Thank you for the hints. 谢谢你的提示。 To solve this problem, you need to tell EF to create a mapping table in the DB. 要解决此问题,您需要告诉EF在数据库中创建映射表。 The reason is because it is a matter of a “Many to Many” problem. 原因是因为这是一个“多对多”问题。

Please consider also my other post . 也请考虑我的其他帖子

    public class ModelConfiguration
    {
        private static void ConfigureGridDataCollectionEntity(DbModelBuilder modelBuilder)
        {
            // Student
            modelBuilder.Entity<Student>()
                .HasMany(p => p.Friends)
                .WithMany()
                .Map(cs =>
                {
                    cs.MapLeftKey("StudentId");
                    cs.MapRightKey("FriendsId");
                    cs.ToTable("StudentFriend");
                });
        }
    }

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

相关问题 包括抽象父EF代码优先对象的列表 - Include list of objects on abstract parent EF Code First EF 5代码首先,具有级联删除功能的多个父级子级? - EF 5 Code First, multiple parent child with cascade delete? EF代码优先+从父级删除子级对象? - EF Code first + Delete Child Object from Parent? EF 4.1父级子代和继承映射的代码优先问题 - EF 4.1 Code First problem with parent child and Inheritance Mapping 多个父实体首先在EF代码中包含一个子实体 - multiple parent Entities with one child entity in EF code first EF Core - 多个父母拥有同一个孩子的列表(代码优先) - EF Core - multiple parents with a list of the same child (code first) EF代码优先从对象列表添加方法 - EF Code First Generic Add Method From List of Objects EF代码优先:尝试获取子属性的参数以显示在父类的表中 - EF Code First: Trying to get a child property's parameters to appear in the parent class' table 我如何首先使用可重用的查询/表达式和EF代码在父级中获取子实体作为DTO? - How can I fetch child entities as DTO in parent using reusable queries/Expression's with EF code first? EF4 Code First:同一张表中的父/子关系,如何获得逆属性? - EF4 Code First: Parent/Child relationship in same table, how to get inverse property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM