PDA

View Full Version : ارتباط با دو کلید خارجی



TeRMiNaT00o00R
یک شنبه 17 آذر 1392, 05:17 صبح
با سلام
برای اینکه در Ef نشان دهیم موجودیتی با موجودیت دیگر با دو کلید خارجی در ارتباط است باید به چه شکل عمل کرد؟
مثلا در ارسال نامه ذر نظر بگیرید فرستنده و دریافت کننده داریم که هر دو از موجودیت کاربر میباشند که میتوانند ارسال و دریافت نماییند . که نیاز ات دو کلید خارجی از کاربر در این جا در نظر گرفته شود .برای پیاده سازی این مورد باید به چه شکل عمل کرد ؟

parvizwpf
یک شنبه 17 آذر 1392, 18:49 عصر
خب این مثل همون یه دونه کید هست شما صد تا کلید خارجی بدید. همون کد داره برای فیلدهای مختلف تکرار میشه.

TeRMiNaT00o00R
دوشنبه 18 آذر 1392, 04:06 صبح
بله درسته اما من وقتی اینکارو انجام

کلاس کاربر را در نظر بگیرید که با کلاس SupplierPartUser با دو کلید در ارتباط است اما موقع ایجاد جداول علاو بر کلید های UserId و InspectorId , کلید های خارجی User_Id و User_Id1 را نیز ایجاد مینمایید مشکل از کجاست؟



public class User:BaseEntity
{
[Key]
public virtual int Id { get; set; }
public virtual ICollection<SupplierPartUser> SupplierPartUsers { get; set; }
public virtual ICollection<SupplierPartUser> Inspectors { get; set; }


{


کلاس SupplierPartUser




public class SupplierPartUser: BaseEntity
{
[Key]
public int SupplierPartUserID { get; set; }
[ForeignKey("UserId")]
public virtual User User { set; get; }
public int? UserId { set; get; }
[ForeignKey("InspectorId")]
public virtual User Inspector { set; get; }
public int? InspectorId { set; get; }
}

parvizwpf
دوشنبه 18 آذر 1392, 19:29 عصر
باید اینکارو بصورت صحیح توی onModelCreating تحریف شده در کلاس کانتکستتون هندل کنید.

aghayex
سه شنبه 19 آذر 1392, 18:43 عصر
به این صورت عمل کن :

public class User:BaseEntity
{
[Key]
public int Id { get; set; }
[inverseproperty("User ")]
public virtual ICollection<SupplierPartUser> SupplierPartUsers { get; set; }

[inverseproperty("Inspector ")]
public virtual ICollection<SupplierPartUser> Inspectors { get; set; }
}



public class SupplierPartUser: BaseEntity
{
[Key]
public int SupplierPartUserID { get; set; }

[inverseproperty("SupplierPartUsers ")]
public virtual User User { set; get; }
public int? UserId { set; get; }

[inverseproperty("Inspectors ")]
public virtual User Inspector { set; get; }
public int? InspectorId { set; get; }
}

اما این ارتباط یک ارباط 0..1 --> * ایجاد می کنه اما من یه ارتباط 1 --> * می خوام و برا همین من می یام مدلمو به این صورت طراحی می کنم :

public class User:BaseEntity
{
[Key]
public int Id { get; set; }
[inverseproperty("User ")]
public virtual ICollection<SupplierPartUser> SupplierPartUsers { get; set; }

[inverseproperty("Inspector ")]
public virtual ICollection<SupplierPartUser> Inspectors { get; set; }
}



public class SupplierPartUser: BaseEntity
{
[Key]
public int SupplierPartUserID { get; set; }

[inverseproperty("SupplierPartUsers ")]
public virtual User User { set; get; }
public int UserId { set; get; }

[inverseproperty("Inspectors ")]
public virtual User Inspector { set; get; }
public int InspectorId { set; get; }
}

کاری که من کردم اومدم نوع کلید خارجی رو از ?int به int تبدیل می کنم اما خطا میده ؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟

aghayex
سه شنبه 19 آذر 1392, 23:04 عصر
با توسل به مادر این مشکل هم حل شد .
برا اینکه بخواهیم ارتباطی داشته باشیم که کلید خارجی نال پذیر نباشد به این صورت عمل می کنیم :

public class Team
{
public int TeamId { get; set;}
public string Name { get; set; }

public virtual ICollection<Match> HomeMatches { get; set; }
public virtual ICollection<Match> AwayMatches { get; set; }
}

public class Match
{
public int MatchId { get; set; }

public int HomeTeamId { get; set; }
public int GuestTeamId { get; set; }

public float HomePoints { get; set; }
public float GuestPoints { get; set; }
public DateTime Date { get; set; }

public virtual Team HomeTeam { get; set; }
public virtual Team GuestTeam { get; set; }
}


public class Context : DbContext
{
...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Match>()
.HasRequired(m => m.HomeTeam)
.WithMany(t => t.HomeMatches)
.HasForeignKey(m => m.HomeTeamId)
.WillCascadeOnDelete(false);

modelBuilder.Entity<Match>()
.HasRequired(m => m.GuestTeam)
.WithMany(t => t.AwayMatches)
.HasForeignKey(m => m.GuestTeamId)
.WillCascadeOnDelete(false);
}
}