PDA

View Full Version : ایجاد ارتباط بین جداول در entity code first



aghayex
جمعه 31 خرداد 1392, 10:40 صبح
با سلام دوستان من در ایجاد ارتباط بین جدول هام دچار ابهاماتی شدم هر چی جزوه هم می خونم باز هم قضیه برام جا نمی یوفته مثلا ما در محیط خود sql اگه یه ستون که کلید بود رو به یه ستون غیر کلید متصل می کردیم می شد رابطه ی یک به چند حالا اگه بخوام همین کارو با کد فیرست انجام بدم نمیشه ؟!


public class Reshte
{

[Key]


public int Id { set; get; }
public string Name { set; get; }
public string Sabk { set; get; }
public virtual ostad ostad { get; set; }
public virtual student student { get; set; }
}


public class student
{
[Key,Column(Order=0)]



public int Id { set; get; }
public string name { set; get; }
public string last { set; get; }


public int reshte { set; get; }
}


public class ostad
{
[Key,Column(Order=0)]



public int Id { set; get; }
public string name { set; get; }
public string last { set; get; }


public int reshte { set; get; }
}


این عکس ضمیمه شده هدف من هست

mo.esmp
جمعه 31 خرداد 1392, 14:04 عصر
public class Reshte
{
public int Id { set; get; }

public string Name { set; get; }

public string Sabk { set; get; }

public virtual IList<ostad> Ostad { get; set; }

public virtual IList<student> Student { get; set; }
}

public class student
{
public int Id { set; get; }

public string name { set; get; }

public string last { set; get; }

public int ReshteId { set; get; }

[ForeignKey("ReshteId")]
public Reshte Reshte { get; set; }
}

public class ostad
{
public int Id { set; get; }

public string name { set; get; }

public string last { set; get; }

public int ReshteId { set; get; }

[ForeignKey("ReshteId")]
public Reshte Reshte { get; set; }
}

aghayex
جمعه 31 خرداد 1392, 23:06 عصر
دوست عزیز من یه جدول دیگه دارم عین جدول Reshte با عنوان state ه می خوام به دو جدول دانش آموز و استاد ارتباطش بدم اما متاسفانه وقتی برنامه اجرا می شه دیتابیس خود به خود ساخته نمیشه اما با سه جدول شما اجرا میشه (البته تنظیمات ساخت اتوماتیک دیتابیس رو انجام دادم)

public class State
{
[Key]
public int Id { set; get; }
public int Shar { set; get; }
public string Name { set; get; }
public virtual IList<Ostad> Ostad { get; set; }
public virtual IList<Student> Student { get; set; }
}

mo.esmp
شنبه 01 تیر 1392, 15:09 عصر
public class student
{
public int Id { set; get; }

public string name { set; get; }

public string last { set; get; }

public int ReshteId { set; get; }

[ForeignKey("ReshteId")]
public Reshte Reshte { get; set; }

public int StateId { set; get; }

[ForeignKey("StateId")]
public State State { get; set; }
}

public class ostad
{
public int Id { set; get; }

public string name { set; get; }

public string last { set; get; }

public int ReshteId { set; get; }

[ForeignKey("ReshteId")]
public Reshte Reshte { get; set; }

public int StateId { set; get; }

[ForeignKey("StateId")]
public State State { get; set; }
}

aghayex
یک شنبه 02 تیر 1392, 19:43 عصر
دوس عزیز تشکر از همکاری تون
حالا اگه ما سه ستون یا سه پروپرتی داشته باشیم و بخواهیم به یک جدول ارتباطش بدیم چطوریه ؟


public class student
{
public int Id { set; get; }
public string name { set; get; }
[ForeignKey("Sex")]
public int SexId { get; set; }
public virtual combo Sex { get; set; }
[ForeignKey("Reshte")]
public int ReshteId { set; get; }
public virtual combo Reshte { get; set; }

[ForeignKey("Kelas")]
public int KelasId { set; get; }
public virtual combo Kelas { get; set; }
}
public class combo
{
public int Id { set; get; }
public byte no { set; get; }
public string name { set; get; }

public virtual ICollection<student> students { get; set; }
}

aghayex
سه شنبه 04 تیر 1392, 16:49 عصر
کسی نیست کمک کنه ؟

aghayex
چهارشنبه 05 تیر 1392, 18:58 عصر
مشکلو این طور حل کردم :

public class student
{
public int Id { set; get; }
public string name { set; get; }
public virtual combo Sex { get; set; }
public virtual combo Reshte { get; set; }
public virtual combo Kelas { get; set; }
}
public class combo
{
public int Id { set; get; }
public byte no { set; get; }
public string name { set; get; }
[InverseProperty("Sex")]
public virtual ICollection<Morabi> MorabianSex { get; set; }
[InverseProperty("Reshte")]
public virtual ICollection<Morabi> MorabianReshte { get; set; }
[InverseProperty("Kelas")]
public virtual ICollection<Morabi> MorabianReshte { get; set; }
}


اما ان تی تی موقعی که ستون ها رو در دیتابیس می سازه میاد یه عبارت رو به انتهای ستونی که ارتباط داده شته اضافه می کنه مثلا برا ستون kelas میاد در دیتابیس این طور درست می کنه kelas_Id که اگه من بخوام اینطور باشه kelasId رو نمی دونم باید چطور باشه