C# 如何建立正确的代码优先关系

C# 如何建立正确的代码优先关系,c#,entity-framework,code-first,C#,Entity Framework,Code First,我对实体框架相当陌生,感觉使用代码优先模式比使用数据库优先模式更能控制 我想知道,在以编程方式设置实体之间的ForeignKey关系时,什么更可取 在与另一个类相关的类中声明FK属性更好,还是在与另一个类相关的类中声明IEnumerable属性更好 public class IRelateToAnotherClass { ... public int FK_IGetRelatedToByAnotherClass_ID { get; set; } } 或 public类IGetR

我对实体框架相当陌生,感觉使用代码优先模式比使用数据库优先模式更能控制

我想知道,在以编程方式设置实体之间的
ForeignKey
关系时,什么更可取

在与另一个类相关的类中声明
FK
属性更好,还是在与另一个类相关的类中声明
IEnumerable
属性更好

public class IRelateToAnotherClass
{
    ...
    public int FK_IGetRelatedToByAnotherClass_ID { get; set; }
}

public类IGetRelatedToByAnotherClass
{
...
与{get;set;}相关的公共IEnumerable
}

这完全取决于您希望实体之间的关系类型(一对一、一对多、多对多);但是,是的,您应该声明外键属性。看看这个网站

这是两个课程的一对多:

public class IRelateToAnotherClass
{
    public int Id { get; set; } // primary key
    public virtual ICollection<IGetRelatedToByAnotherClass> IGetRelatedToByAnotherClasses { get; set; }
}

public class IGetRelatedToByAnotherClass
{
    public int Id { get; set; } // primary key
    public int IRelateToAnotherClassId { get; set; } // foreign key
    public virtual IRelateToAnotherClass IRelateToAnotherClass { get; set; }
}
public类IRelatetoother类
{
public int Id{get;set;}//主键
公共虚拟ICollection IGETRElatedTobyanotherClass{get;set;}
}
公共类IGetRelatedToByAnotherClass
{
public int Id{get;set;}//主键
public int irelatetootherClassID{get;set;}//外键
公共虚拟IRelateToAnotherClass IRelateToAnotherClass{get;set;}
}
通过一些流畅的API映射:

modelBuilder.Entity<IGetRelatedToByAnotherClass>.HasRequired<IRelateToAnotherClass>(p => p.IRelateToAnotherClass).WithMany(p => p.IGetRelatedToByAnotherClasses).HasForeignKey(p => p.Id);
modelBuilder.Entity.HasRequired(p=>p.irelatetoother类)。包含许多(p=>p.igetrelatedtobyanotherclass)。HasForeignKey(p=>p.Id);

这完全取决于您希望实体之间的关系类型(一对一、一对多、多对多);但是,是的,您应该声明外键属性。看看这个网站

这是两个课程的一对多:

public class IRelateToAnotherClass
{
    public int Id { get; set; } // primary key
    public virtual ICollection<IGetRelatedToByAnotherClass> IGetRelatedToByAnotherClasses { get; set; }
}

public class IGetRelatedToByAnotherClass
{
    public int Id { get; set; } // primary key
    public int IRelateToAnotherClassId { get; set; } // foreign key
    public virtual IRelateToAnotherClass IRelateToAnotherClass { get; set; }
}
public类IRelatetoother类
{
public int Id{get;set;}//主键
公共虚拟ICollection IGETRElatedTobyanotherClass{get;set;}
}
公共类IGetRelatedToByAnotherClass
{
public int Id{get;set;}//主键
public int irelatetootherClassID{get;set;}//外键
公共虚拟IRelateToAnotherClass IRelateToAnotherClass{get;set;}
}
通过一些流畅的API映射:

modelBuilder.Entity<IGetRelatedToByAnotherClass>.HasRequired<IRelateToAnotherClass>(p => p.IRelateToAnotherClass).WithMany(p => p.IGetRelatedToByAnotherClasses).HasForeignKey(p => p.Id);
modelBuilder.Entity.HasRequired(p=>p.irelatetoother类)。包含许多(p=>p.igetrelatedtobyanotherclass)。HasForeignKey(p=>p.Id);

如果我正确理解了您的要求,您将希望两者兼而有之。您希望使用int-FK属性和object属性作为导航属性

最终结果如下所示:

public class Employee
{
    [Key]
    public int EmployeeID { get; set; }

    [ForeignKey("Store")]
    public int StoreNumber { get; set; }

    // Navigation Properties
    public virtual Store Store { get; set; }
}

public class Store
{
    [Key]
    public int StoreNumber { get; set; }

    // Navigation Properties   
    public virtual List<Employee> Employees { get; set; }
}
公共类员工
{
[关键]
public int EmployeeID{get;set;}
[外键(“存储”)]
public int StoreNumber{get;set;}
//导航属性
公共虚拟存储{get;set;}
}
公共类商店
{
[关键]
public int StoreNumber{get;set;}
//导航属性
公共虚拟列表雇员{get;set;}
}


如果您还没有,请查看和延迟加载。请注意,EF足够聪明,可以判断int StoreID属性对应于对象存储属性,但如果它们的名称不同(例如没有ID后缀),则必须使用。

如果我正确理解您的要求,您需要两者。您需要一个int-FK属性和一个object属性用作导航属性

最终结果如下所示:

public class Employee
{
    [Key]
    public int EmployeeID { get; set; }

    [ForeignKey("Store")]
    public int StoreNumber { get; set; }

    // Navigation Properties
    public virtual Store Store { get; set; }
}

public class Store
{
    [Key]
    public int StoreNumber { get; set; }

    // Navigation Properties   
    public virtual List<Employee> Employees { get; set; }
}
公共类员工
{
[关键]
public int EmployeeID{get;set;}
[外键(“存储”)]
public int StoreNumber{get;set;}
//导航属性
公共虚拟存储{get;set;}
}
公共类商店
{
[关键]
public int StoreNumber{get;set;}
//导航属性
公共虚拟列表雇员{get;set;}
}


如果您还没有,请查看和延迟加载。请注意,EF足够聪明,可以判断int StoreID属性对应于对象存储属性,但如果它们的名称不同(例如没有ID后缀),则必须使用。

感谢您的链接和解释。感谢您的链接和解释。