简体   繁体   English

如何使用数据注释在实体框架中设置外键?

[英]How to set up foreign key in Entity Framework with data annotations?

I am trying to build a web application that connects to a SQL Server using Entity Framework.我正在尝试构建一个 web 应用程序,该应用程序使用实体框架连接到 SQL 服务器。 I have three classes, one is to register consultations, the second one is for registering contact information and the last one is to classify consultations types (kind of like an enumerable but I need to store more data).我有三个类,一个是注册咨询,第二个是注册联系信息,最后一个是分类咨询类型(有点像可枚举但我需要存储更多数据)。

Every consultation has a contact information (so there is a reference to the primary key of table Contact ) and a ConsultationType (another reference to the primary key of table Type ).每个咨询都有一个联系信息(因此有对表Contact的主键的引用)和一个ConsultationType (对表Type的主键的另一个引用)。

public class Consultation
{
    [Key]
    public long id { get; set; }

    public long ContactID { get; set; }
    [ForeignKey("ContactID")]
    public Contact Contact { get; set; }

    public long ConsultationTypeID{ get; set; }
    [ForeignKey("ConsultationTypeID")]
    public ConsultationType Type { get; set; }
}

public class Contact
{
    public long id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

public class ConsultationType
{
    public long id { get; set; }
    public string type{ get; set; }
    public string abbreviated{ get; set; }
} 

The Consultation table has 3 columns ( id , ContactID and ConsultationTypeID ). Consultation表有 3 列( idContactIDConsultationTypeID )。 I am expecting all the Contact and ConsultationType references to be filled automatically based on the ID in the foreign keys.我希望根据外键中的 ID 自动填充所有ContactConsultationType引用。

Currently when I retrieve data from the database, only the ConsultationType gets filled, not the contact.目前,当我从数据库中检索数据时,只填充了ConsultationType ,而不是联系人。 I can not understand this behaviour because I think I have all set up the same way.我无法理解这种行为,因为我认为我的设置方式都相同。

EDIT : when I retrieve Consultation data, I get a list of objects with 5 properties (consultation ID, contact ID, contact, consultation type Id, consultation type).编辑:当我检索Consultation数据时,我得到一个包含 5 个属性的对象列表(咨询 ID、联系人 ID、联系人、咨询类型 ID、咨询类型)。 4 of them are set (only contact is not set).其中4个已设置(仅联系人未设置)。 I want the contact property to be set to an instance of the row that contains the ID referenced by the Contact ID property.我希望将联系人属性设置为包含联系人 ID 属性引用的 ID 的行的实例。

Rereading the select part.重读 select 部分。

consultationTypes = await _context.ConsultationTypes.ToListAsync();
Consultation = await _context.Consultations.ToListAsync();

I thought that maybe I should load the Contact also.我想也许我也应该加载联系人。 So I have added:所以我补充说:

await _context.ConsultationTypes.ToListAsync();
await _context.Contacts.ToListAsync();
Consultation = await _context.Consultations.ToListAsync();

I don't really understand why I need to do it, but it is working now.我真的不明白为什么我需要这样做,但它现在正在工作。 I wonder if there is a way to load all tables so I don't have to do it every time.我想知道是否有办法加载所有表,所以我不必每次都这样做。

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

相关问题 实体框架 - 代码优先 - 数据注释 - 不必要的外键列 - Entity Framework - Code first - Data annotations - Unnecessary foreign key columns 如何在带有数据注释的实体框架中检索外键关系中的列子集 - How to retrieve a subset of columns in a foreign-key relationship in Entity Framework with Data Annotations 如何使用实体框架在FluentAPI /数据注释中定义外键可选关系? - How do I define Foreign Key Optional Relationships in FluentAPI/Data Annotations with the Entity Framework? Entity Framework 实体如何设置 ID 和外键约束? - Entity Framework entity how to set ID and foreign key constraint? 我可以使用Entity Framework Code First数据注释创建双向外键关系吗? - Can I create a bidirectional foreign key relationship using Entity Framework Code First data annotations? 在实体框架中建立外键关系 - Setting up foreign key relationship in Entity Framework 如何在Entity Framework代码优先中设置空外键 - How to set null foreign key in Entity Framework code-first 我们如何在实体框架中将列设置为外键 - How do we set column as foreign key in Entity Framework 如何使用实体框架和FluentHtml设置外键对象 - How to set Foreign Key object using Entity Framework and FluentHtml 如何在Entity Framework 6的数据批注中添加复合外键 - How to add composite foreign key in data annotation for Entity Framework 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM