[英]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 列( id
、 ContactID
和ConsultationTypeID
)。 I am expecting all the Contact
and ConsultationType
references to be filled automatically based on the ID in the foreign keys.我希望根据外键中的 ID 自动填充所有Contact
和ConsultationType
引用。
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.