PDA

View Full Version : سوال: اجباری بودن کلید خارجی



kamran_14
جمعه 29 تیر 1397, 16:16 عصر
سلام
چرا وقتی 2 تا از کلیدخارجیها اجباری میشوند برنامه هنگام اجرا خطا میده ولی وقتی یکیش اختیاری میشه دیگه خطا نمیده؟
خطا این است.

Public class tblMessage
{
public int ID { get; set; }
……………
……………
[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblMessages")]
[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("UserSend")]
public virtual tblUser tblUser { get; set; }

[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblMessages2")]
[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("UserGet")]
public virtual tblUser tblUser2 { get; set; }
}

Public class tblUser {
public int ID { get; set; }
……………
……………
[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblUser")]
public virtual List<tblMessage> tblMessages { get; set; }

[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblUser2")]
public virtual List<tblMessage> tblMessages2 { get; set; }
}



148579

ali_md110
جمعه 29 تیر 1397, 19:20 عصر
نحوه تعریف کلید خارجی اشتباه هست


[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("UserSend")]

این UserSend کجاست؟

یا UserGet وجود ندارد

در ضمن InverseProperty در جدول فرزند الزامی ندارد تعریف کنید و اینجا هم باز اشتباه تعریف کردید

kamran_14
شنبه 30 تیر 1397, 11:23 صبح
نحوه تعریف کلید خارجی اشتباه هست


[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("UserSend")]

این UserSend کجاست؟

یا UserGet وجود ندارد

در ضمن InverseProperty در جدول فرزند الزامی ندارد تعریف کنید و اینجا هم باز اشتباه تعریف کردید

ببخشید یادم رفته بود اونا رو هم بنویسم:

public class tblMessage {
public tblMessage()
{

}


[System.ComponentModel.DataAnnotations.Schema.Datab aseGenerated(System.ComponentModel.DataAnnotations .Schema.DatabaseGeneratedOption.None)]
[System.ComponentModel.DataAnnotations.Key]
[System.ComponentModel.DataAnnotations.Required]
public int ID { get; set; }


//[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("tblUser")]
[System.ComponentModel.DataAnnotations.Required]
public int UserGet { get; set; }


//[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("tblUser")]
public int? UserSend { get; set; }


[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblMessages")]
[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("UserSend")]
public virtual tblUser tblUser { get; set; }


[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblMessages2")]
[System.ComponentModel.DataAnnotations.Schema.Forei gnKey("UserGet")]
public virtual tblUser tblUser2 { get; set; }
}


public class tblUser
{
public tblUser()
{


}


[System.ComponentModel.DataAnnotations.Schema.Datab aseGenerated(System.ComponentModel.DataAnnotations .Schema.DatabaseGeneratedOption.None)]
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.Key]
public int ID { get; set; }

[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblUser")]
public virtual List<tblMessage> tblMessages { get; set; }


[System.ComponentModel.DataAnnotations.Schema.Inver seProperty("tblUser2")]
public virtual List<tblMessage> tblMessages2 { get; set; }
}

ببخشید من تازه asp.net mvc رو یاد میگیرم. کلاس فرزند کدومه؟
آیا نحوه تعریف کلید خارجی اشتباه است؟
مگه نگفتید الزامی ندارد به تعریف Inversproperty در کلاس فرزند، پس چرا در صورت تعریف گفتید اشتباه است؟