我只想在user和userdetails之间建立一对一的关系。首先使用实体框架代码。到目前为止我做了什么==>
public class User
{
[Key]
[Required(AllowEmptyStrings = false, ErrorMessage = "User Id Is Required!")]
[MaxLength(12, ErrorMessage = "You Have Exceed The Max length Of User ID which is [12] character!")]
[RegularExpression("[0-9]{2}-[0-9]{5}-[123]{1}|[0-9]{2}-[0-9]{7}-[123]{1}", ErrorMessage = "Invalid Id,It should [xx]-[xxxxx]-[x] or [xx]-[xxxxxxx]-[x]!")]
[Display(Name = "User ID")]
public string UserId { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Password Is Required!")]
[MaxLength(20, ErrorMessage = "Password Max Length Is 20 Character!")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string password { get; set; }
/// <summary>
///
/// </summary>
[MaxLength(100, ErrorMessage = "The Max Length For User Level Is 100 Character!")]
[RegularExpression("^(?:admin|Admin|student|Student)$", ErrorMessage = "Invalid User Level!")]
[Display(Name = "User Level")]
public string level { get; set; }
/// <summary>
///
/// </summary>
[NotMapped]
[Display(Name="Remember Me")]
public bool RememberMe { get; set; }
//relationship with other table----
public virtual UserDetail UserDetail { get; set; }
public virtual ICollection<Financeofstudent> Financeofstudents { get; set; }
public virtual ICollection<Studentassignbatche> Studentassignbatches { get; set; }
[NotMapped]
public StudentRegistrationViewModel StudentRegistrationViewModel { get; set; }
}
此处UserId
是主键。这是用户在注册时提供的。
和我的userdetails表==&gt;
public class UserDetail
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
/// <summary>
///
/// </summary>
[MaxLength(12, ErrorMessage = "You Have Exceed The Max length Of User ID which is 12 Character!")]
[RegularExpression("[0-9]{2}-[0-9]{5}-[123]{1}|[0-9]{2}-[0-9]{7}-[123]{1}", ErrorMessage = "Invalid Id,It should [xx]-[xxxxx]-[x] or [xx]-[xxxxxxx]-[x]")]
[Display(Name = "User ID")]
public string UserId { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Full Name Is Required!")]
[MaxLength(100, ErrorMessage = "The Max Length Of Full Name Is 100 Character!")]
[DataType(DataType.Text, ErrorMessage = "Invalid Full Name!")]
[Display(Name = "Full Name")]
public string fullname { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "First Name IS Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of First Name Is 50 Character!")]
[DataType(DataType.Text, ErrorMessage = "Invalid First Name!")]
[Display(Name = "First Name")]
public string first_name { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Last Name Is Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of Last Name is 50 Character!")]
[DataType(DataType.Text, ErrorMessage = "Invalid Last Name!")]
[Display(Name = "Last Name")]
public string last_name { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Pic Is Required!")]
[MaxLength(700, ErrorMessage = "Your Destined Pic Path Is Too Much Long.The Max Length Of Picture Path Is 700 Character!")]
[Display(Name = "Profile Pic")]
public string pic_path { get; set; }
/// <summary>
///
/// </summary>
[Display(Name = "Company Name")]
public string company_name { get; set; }
/// <summary>
///
/// </summary>
[Display(Name = "City")]
public string city { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Phone Number Is Required!")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number!")]
[Display(Name = "Phone Number")]
[Remote("IsPhoneNumberAvailable", "Students", AdditionalFields = "InitialPhoneNumberValue", ErrorMessage = "Phone Number IS Already Taken! Please Choose A Unique One!")]
public int phone_number { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Email Is Required!")]
[EmailAddress(ErrorMessage = "Invalid Email Address!")]
[Display(Name = "Email Address")]
[Remote("IsEmailAvailable", "Students", AdditionalFields = "InitialEmailValue", ErrorMessage = "Email Address IS Already Taken! Please Choose A Unique One!")]
public string email { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Zip Code Is Required!")]
[RegularExpression("^[0-9]{4,5}$", ErrorMessage = "Invalid Zip Code!")]
[Display(Name = "Zip Code")]
public int zip_code { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Nationality Is Required!")]
[MaxLength(30, ErrorMessage = "The Max Length Of Nationality Is 30 Character!")]
[DataType(DataType.Text, ErrorMessage = "Invalid Nationality!")]
[Display(Name = "User Nationality")]
public string nationality { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Gender Is Required!")]
[RegularExpression("^(?:m|M|male|Male|f|F|female|Female)$", ErrorMessage = "Invalid Gender!")]
[Display(Name = "Gender")]
public string sex { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Religion Is Required!")]
[MaxLength(20, ErrorMessage = "The Max Length Of Religion is 20 Character!")]
[DataType(DataType.Text, ErrorMessage = "Invalid Religion!")]
[RegularExpression("^[a-zA-Z]*$", ErrorMessage = "Invalid Religion!")]
[Display(Name = "Religion")]
public string religion { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Blood Group Is Required!")]
[MaxLength(50, ErrorMessage = "The Max Length Of Blood Group Is 50 character!")]
[Display(Name = "Blood Group")]
public string blood_group { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "Date Of Birth Is Required!")]
[DataType(DataType.Date, ErrorMessage = "Invalid Date Of Birth")]
[Display(Name = "Date Of Birth")]
public DateTime dob { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "User Activation Is Required!")]
[DataType(DataType.Date, ErrorMessage = "Invalid Date!")]
[Display(Name = "User Activation Date")]
public DateTime user_activation_date { get; set; }
/// <summary>
///
/// </summary>
[DataType(DataType.Date, ErrorMessage = "Invalid Date!")]
[Display(Name = "User Deactivation Date")]
public Nullable<DateTime> user_deactivation_date { get; set; }
/// <summary>
///
/// </summary>
[Required(AllowEmptyStrings = false, ErrorMessage = "User Current Status Is Required!")]
[RegularExpression("^(?:active|Active|inactive|Inactive)$", ErrorMessage = "Invalid Status!")]
[Display(Name = "User Current Status")]
public string user_active { get; set; }
//relationshipwith other tables-----
public virtual User User { get; set; }
}
<{1}}中的是主键并自动生成。
乳清我正在尝试构建它,它给我一个错误,如下面==&gt;
userdetails
我使用Documentation来编写代码。
尝试使用Unable to determine the principal end of an association between the types 'TSMS_Atp_2_Final_Project.Models.Com.Tsms.UserDetail' and 'TSMS_Atp_2_Final_Project.Models.Com.Tsms.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
中的某种不同方式,如下所示==&gt;
userdetails
但它也会出现类似下面的错误=&gt;
该物业&#39; UserId&#39;无法配置为导航属性。该属性必须是有效的实体类型,并且该属性应具有非抽象的getter和setter。对于集合属性,类型必须实现ICollection,其中T是有效的实体类型。