[英]Foreign Key Relationship EF Code First
I'm busy creating my first EF code first model and I've come across a slightly confusing problem. 我正在忙于创建我的第一个EF代码优先模型,并且遇到了一个令人困惑的问题。
I have a number of model classes that each inherit from a base model class that has three common properties I want used in all model classes. 我有许多模型类,每个模型类都继承自一个基础模型类,该基础模型类具有我想在所有模型类中使用的三个公共属性。 These properties are Id, LastUpdated and LastUpdatedBy. 这些属性是Id,LastUpdated和LastUpdatedBy。
Id is the primary key of each model class. ID是每个模型类的主键。 LastUpdated is a foreign key to my 'User' model class. LastUpdated是我的“用户”模型类的外键。 LastUpdatedBy is a datetime field that indicates the last time the record was modified. LastUpdatedBy是日期时间字段,指示上次修改记录的时间。
So what I'd like to setup is the 1 to 1 foreign key relationship from my base class to my 'User' model class but I'm receiving the exception: 因此,我想设置的是从我的基类到“用户”模型类的一对一外键关系,但是我收到了异常:
Multiplicity is not valid in Role 'Profile_LastUpdatedByUser_Source' in relationship 'Profile_LastUpdatedByUser'. 多重性在关系“ Profile_LastUpdatedByUser”中的角色“ Profile_LastUpdatedByUser_Source”中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*' 由于从属角色属性不是关键属性,因此从属角色多重性的上限必须为“ *”
This is my ModelBase class: 这是我的ModelBase类:
public class ModelBase
{
public int Id { get; set; }
public DateTime LastUpdated { get; set; }
[ForeignKey("LastUpdatedByUser")]
[Required]
public int LastUpdatedByUserId { get; set; }
public virtual User LastUpdatedByUser { get; set; }
}
This is one of my model classes: 这是我的模型课程之一:
public class Profile : ModelBase
{
[StringLength(25, MinimumLength=1)]
[Required(ErrorMessage="First Name is Required")]
public string FirstName { get; set; }
[StringLength(25, MinimumLength = 1)]
[Required(ErrorMessage = "Last Name is Required")]
public string LastName { get; set; }
[StringLength(25, MinimumLength = 1)]
[Required(ErrorMessage = "Email Address is Required")]
public string Email { get; set; }
public string Mobile { get; set; }
public string HomePhone { get; set; }
public string WorkPhone { get; set; }
public string ImageSource { get; set; }
public Squable.Model.Enums.MembershipType.MembershipTypeEnum MembershipType { get; set; }
}
This is my user class (Please ignore the Password property, I'll fix that later ;) ): 这是我的用户类(请忽略Password属性,稍后再解决;)):
public class User : ModelBase
{
public string UserName { get; set; }
public string Password { get; set; }
public int ProfileId { get; set; }
public virtual Profile Profile { get; set; }
}
I don't know if what I am doing is best practise but I could do with some advice as to how to either fix the problem or maybe just some pointers in the right direction. 我不知道我在做什么是否是最佳实践,但是我可以就如何解决问题或仅向正确方向提出一些建议。
Move 移动
public int Id { get; set; }
to User class and to Profile you can also change the names to UserId and to ProfileId and move 到User class和Profile,还可以将名称更改为UserId和ProfileId并移动
public virtual User LastUpdatedByUser { get; set; }
to Profile class. 到Profile类。
I have a bad experience with sharing Id in base entity If you are planning to use Repository and UnitOfWork pattern you will get a lot of problems later. 我在基本实体中共享ID的经验很不好,如果您打算使用Repository和UnitOfWork模式,那么以后会遇到很多问题。 Check your current database structure and tables with SQL Server Management Studio. 使用SQL Server Management Studio检查当前的数据库结构和表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.