简体   繁体   English

实体框架一对多和多对多关系

[英]Entity Framework one-to-many and many-to-many relationship

im currently learning the entity framework and been miserably failing with this task for a couple of days... I would like to achieve the following Database Scheme: 即时通讯目前正在学习实体框架,并且在几天内完成了这项任务的失败...我希望实现以下数据库方案:

TableA:
ClassA_ID
ClassB_ID(foreign key - one to one)

TableATableB:(one-to-many relationship)
ClassA_ID
ClassB_ID

TableB:
ClassA_ID(foreign key - one to one)
ClassB_ID

So I Actually want a specific one to one relationship from Class A to Class B and a one to many Relationship from ClassA to ClassB. 所以我实际上想要从A类到B类的特定的一对一关系以及从ClassA到ClassB的一对多关系。

My C# Code Looks like this: 我的C#代码看起来像这样:

Class A: A类:

public static int idcounter;
[Key]
public int id { get; set; }
public virtual List<ClassB> allClassB { get; set; }
public virtual ClassB currentClassB { get; set; }
public int? currentClassBID { get; set; }

public ClassA(){
    idcounter++;
    id = idcounter;
    allClassB = new List<ClassB>();
    currentClassB = new ClassB();
    currentClassBID = currentClassB.id;
    MyContext.add(this);
}

Class B: B级:

public static int idcounter;
[Key]
public int id { get; set; }
public virtual ClassA owner { get; set; }
public int? ownerID { get; set; }

public ClassB(ClassA a){
    idcounter++;
    id = idcounter;
    owner = a;
    ownerID =  a.id;
}

MyContext: MyContext:

public static void add(ClassA a)
    {
        using (MyContext context = new MyContext ())
        {
            context.setB.Add(a.currentClassB);
            context.setA.Add(a);
            context.SaveChanges();           
        }
    }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ClassA>().HasOptional(x => x.currentClassB ).WithMany().HasForeignKey(x => x.currentClassBID ).WillCascadeOnDelete(false);
        modelBuilder.Entity<ClassB>().HasOptional(x => x.ClassA).WithMany(x => x.allClassB).HasForeignKey(x => x.ownerID).WillCascadeOnDelete(false) ;
    }

Its pretty similar to this question Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables? 它非常类似于这个问题实体框架代码第一:如何在两个表之间创建一对多和一对一的关系? but I just get a Circular Exception when saving. 但是我在保存时只收到循环异常。

Unable to determine a valid ordering for dependent operations. Dependencies may 
exist due to foreign key constraints, model requirements, or store-generated 
values.

Its pretty weird that after 之后非常奇怪

context.setA.Add(a);

it adds currentClassB into allClassB without me ever mention that... it Even suddenly does that when i dont 它将currentClassB添加到allClassB中,而我没有提到过......它甚至在我不这样做时突然做到了

allClassB = new List<ClassB>();

What am I doing wrong? 我究竟做错了什么?

You have to use InverseProperty as shown below. 您必须使用InverseProperty ,如下所示。

[InverseProperty("allClassB")]
public virtual ClassB currentClassB { get; set; }
public int? currentClassBID { get; set; }

DataAnnotations - InverseProperty Attribute: DataAnnotations - InverseProperty属性:

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

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