简体   繁体   English

如何在EF数据注释中指定外表键?

[英]How to specify foreign table key in EF Data Annotations?

I'm trying to configure relations between tables using Data Annotations of Entity Framework. 我正在尝试使用Entity Framework的数据注释配置表之间的关系。 I've found the following example : 我发现以下示例

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

     public int StdandardRefId { get; set; }

    [ForeignKey("StandardRefId")]
    public virtual Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    {
        StudentsList = new List<Student>();
    }
    public int StandardId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

Now, the ForeignKey attribute informs, that StandardRefId is a foregin key. 现在, ForeignKey属性会通知StandardRefId是一个前瞻键。 I guess, that EF deduces the target table from type of property ( Standard ). 我猜想,EF是从属性类型( Standard )推导出目标表的。 However, I fail to see, how to define, which column the foreign key refers to. 但是,我看不到如何定义外键指的是哪一列。 I tried: 我试过了:

[Column("CompanyId")]
public int CompanyId { get; set; }

[ForeignKey("CompanyId")]
[InverseProperty("Id")]
public CompanyDAL Company { get; set; }

However, all I got was the following exception: 但是,我得到的只是以下异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll EntityFramework.dll中发生了类型为'System.InvalidOperationException'的未处理异常

Additional information: The property 'Id' cannot be configured as a navigation property. 附加信息:无法将属性“ Id”配置为导航属性。 The property must be a valid entity type and the property should have a non-abstract getter and setter. 该属性必须是有效的实体类型,并且该属性应具有非抽象的getter和setter。 For collection properties the type must implement ICollection where T is a valid entity type. 对于集合属性,该类型必须实现ICollection,其中T是有效的实体类型。

How can I explicitly say, that CompanyId points to Id property of Company table? 我如何明确地说, CompanyId指向Company表的Id属性?

A Foreign Key will always refer to a Primary Key. 外键将始终引用主键。 When you create a navigation property to Company, that FK will refer to the PK of Company. 当创建公司的导航属性时,该FK将引用公司的PK。

Entity Framework relies on every entity having a key value that it uses for tracking entities. 实体框架依赖于每个具有用于跟踪实体的键值的实体。 One of the conventions that code first depends on is how it implies which property is the key in each of the code first classes. 代码首先依赖的一种约定是它如何暗示哪个属性是每个代码优先类的键。 That convention is to look for a property named “Id” or one that combines the class name and “Id”, such as “BlogId”. 该约定是寻找一个名为“ Id”的属性,或者一个将类名和“ Id”组合在一起的属性,例如“ BlogId”。 The property will map to a primary key column in the database. 该属性将映射到数据库中的主键列。

Source 资源

What this means is that unless you have specified a different PK for Company, the "Id" property (and column) will be the PK for that entity. 这意味着除非您为Company指定了不同的PK,否则“ Id”属性(和列)将是该实体的PK。


Note: For the example you found, Standard uses the second convention for PK ie Id 注意:对于您发现的示例,Standard对PK使用第二种约定,即Id

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

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