简体   繁体   English

非标准命名非唯一外键

[英]Non-standard named non-unique foreign keys

Due to the way that my database is set up (which I don't have control over), I've got a foreign key situation that I can't figure out in Entity Framework. 由于数据库的设置方式(我无法控制),因此我遇到了外键问题,我无法在Entity Framework中弄清楚。 I'm using Fluent API to configure my relationships. 我正在使用Fluent API来配置我的关系。 My classes are 我的课是

public class Parent
{
   // Unique primary key
   public int PrimaryKey { get; set; }

   // These two fields together compose the foreign key to join to Child
   public string ParentForeignKey1 { get; set; }
   public string ParentForeignKey2 { get; set; }

   public List<Child> Children { get; set; }
}

public class Child
{
   // Unique primary key
   public int PrimaryKey { get; set; }

   // These two fields together compose the (non-unique) foreign key to join to Parent
   public string ChildForeignKey1 { get; set; }
   public string ChildForeignKey2 { get; set; }

   // The parent/child relationship is implicit, even though the tables
   // themselves indicate a many-to-many relationship
   public List<Parent> Parents { get; set; }
}

The Entity Framework relationship should be such that the two tables join on the ParentForeignKey and ChildForeignKey fields like 实体框架关系应使两个表在ParentForeignKeyChildForeignKey字段上ParentForeignKey ,例如

SELECT * 
FROM Parent 
JOIN Child 
 ON  Child.ChildForeignKey1 = Parent.ParentForeignKey1
 AND Child.ChildForeignKey2 = Parent.ParentForeignKey2

How do I set up the Fluent API foreign key mapping so that Entity Framework generates those joins when I query the DbSet s? 如何设置Fluent API外键映射,以使Entity Framework在查询DbSet时生成那些联接?

I think this makes more sense 我认为这更有意义

public class Parent
{
   // The parent/child relationship is implicit, even though the tables
   // themselves indicate a many-to-many relationship
   public static List<Parent> Parents { get; set; }


   // Unique primary key
   public int PrimaryKey { get; set; }

   // These two fields together compose the foreign key to join to Child
   public string ParentForeignKey1 { get; set; }
   public string ParentForeignKey2 { get; set; }

   public List<Child> Children { get; set; }
}

public class Child
{
   // Unique primary key
   public int PrimaryKey { get; set; }

   // These two fields together compose the (non-unique) foreign key to join to Parent
   public string ChildForeignKey1 { get; set; }
   public string ChildForeignKey2 { get; set; }

}

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

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