C# 非标准命名非唯一外键

C# 非标准命名非唯一外键,c#,entity-framework,C#,Entity Framework,由于数据库的设置方式(我无法控制),我遇到了一个在实体框架中无法理解的外键情况。我正在使用fluentapi来配置我的关系。我的课是 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 Parent

由于数据库的设置方式(我无法控制),我遇到了一个在实体框架中无法理解的外键情况。我正在使用fluentapi来配置我的关系。我的课是

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; }
}

如何设置Fluent API外键映射,以便实体框架在查询
DbSet
s时生成这些连接?

我认为这更有意义

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; }

}
公共类父类
{
//父/子关系是隐式的,即使表
//它们本身表示一种多对多关系
公共静态列表父项{get;set;}
//唯一主键
public int PrimaryKey{get;set;}
//这两个字段一起构成要连接到子级的外键
公共字符串ParentForeignKey1{get;set;}
公共字符串ParentForeignKey2{get;set;}
公共列表子项{get;set;}
}
公营儿童
{
//唯一主键
public int PrimaryKey{get;set;}
//这两个字段一起构成(非唯一)外键以连接到父级
公共字符串ChildForeignKey1{get;set;}
公共字符串ChildForeignKey2{get;set;}
}

您只有一位家长。你不应该在代码中的某个地方有一个列表吗?很好。我会更新这个问题。我没有把它放进去,因为父/子关系是隐式的(每个子级只有一个父级,即使SQL连接实际上是多对多的)。将静态属性放在
parent
中做什么?为父类的每个实例创建一个公共列表对象。这两个类代表您的最终结果。子类输入来自哪里?父类中的子类是链接,但没有子类的输入列表。我想你也需要一个静态的儿童列表,也许我在最初的问题中不清楚。我想知道如何设置外键关系(
…HasForeignKey()…
),以便在实体框架生成SQL以将父级连接到子级(来自LINQ语句)时,它会像我在示例中所做的那样连接表。您需要两个列表作为输入。输入列表在哪里?
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; }

}