2017-05-08 10 views
0

私はちょうど2つのテーブルの間に1つの関係を作りたいと思っていますテーブルがUSERであり、別のテーブルがUSERDETAILSであり、USERテーブルが主キーUSERIDとUSERDETAILSを持っていますテーブルにはキーID [autogenerated]とforeihgn key referanceユーザーテーブルを使用して...しかし、それは私がそれを作成しようとするといくつかのエラーになります。エンティティフレームワークコードを使用して1対1の関係にする

モデル=> ユーザテーブル

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("[1-3]{2}-[0-9]{5}-[123]{1}|[1-3]{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> 
    [Required(AllowEmptyStrings = false, ErrorMessage = "User Level Is Required!")] 
    [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; } 

    //relationship with other table---- 

    public UserDetail UserDetail { get; set; } 
} 

とuserdetails tablr =>

public class UserDetail 
{ 
    [Key,Column(Order = 0)] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 
    /// <summary> 
    /// ///////////////////// 
    /// </summary> 
    [Key,Column(Order = 1)] 
    [MaxLength(12, ErrorMessage = "You Have Exceed The Max length Of User ID which is 12 Cahracter!")] 
    [RegularExpression("[1-3]{2}-[0-9]{5}-[123]{1}|[1-3]{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!")] 
    [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!")] 
    [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!")] 
    [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!")] 
    [RegularExpression("[0-9]{7,11}", ErrorMessage = "Invalid Phone Number!")] 
    [Display(Name = "Phone Number")] 
    public int phone_number { get; set; } 
    /// <summary> 
    /// ///////////// 
    /// </summary> 
    [Required(AllowEmptyStrings = false, ErrorMessage = "Email Is Required!")] 
    [EmailAddress(ErrorMessage = "Invalid Email Address!")] 
    [Display(Name = "Email Address")] 
    public string email { get; set; } 
    /// <summary> 
    /// ////////// 
    /// </summary> 
    [Required(AllowEmptyStrings = false, ErrorMessage = "Zip Code Is Required!")] 
    [RegularExpression("[0-9]{4}", 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!")] 
    [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!")] 
    [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----- 

    [ForeignKey("UserId")] 
    public User User { get; set; } 
} 

とエラー=>

Error と私は、ユーザーテーブル

を変更する=>してみてください
public List<UserDetail> UserDetail { get; set; } 

答えて

0
.....それは私のために動作しますが、それは、多くの関係に==> enter image description here

1を作成します...しかし、私は1つが私please..orどんな提案をone.helpしたいです

1対1の場合、データベースはどのエンドがプリンシパルであり、どれが依存であるかを知る必要があります。その理由は、どちらか一方が最初に追加されなければならず、両者が互いに依存している場合には起こり得ないからです。あなたのUserクラスをプリンシパルにして、UserDetailsクラスを従属クラスにすることをお勧めします。しかし、流暢な設定を使わずにこれを定義する方法はわかりません。あなたの文脈で次に

public class User 
{ 
    ... 

    public class Config : EntityTypeConfiguration<User> 
    { 
     HasRequired(m => m.UserDetails).WithRequiredPrincipal(m => m.User) 
    } 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Configurations.Add(new User.Config()); 
} 
関連する問題