PDA

View Full Version : سوال در ارتباط با entity framework .net core ارتباط یک به چند



Reza Safa
پنج شنبه 12 فروردین 1400, 16:25 عصر
سلام خدمت همه دوستان
من قبل از اینکه با .net core کار کنم مشکلی در تعریف مدل ها و ارتباط یک به چند نداشتم
اما در .net core همون مدل با رفرنس ها هم می سازم یک آیتم خدوکار می سازه


public partial class ProductAbility
{
[Key]
public int ProductAbilityID { get; set; }
[Display(Name = "قابلیت")]
public string Ability { get; set; }
[DataType(DataType.MultilineText)]
[Display(Name = "محتوا")]
public String Body { get; set; }
[DataType(DataType.Date)]
[Display(Name = "تاریخ")]
public DateTime ReleaseDate { get; set; }
public ICollection<Products> listProductAbility { get; set; }
}
public partial class ProductTypes
{
[Key]
public int ProductTypeID { get; set; }
[Display(Name = "نوع محصول")]
public string Title { get; set; }


[DataType(DataType.Date)]
[Display(Name = "تاریخ")]
public DateTime ReleaseDate { get; set; }
public ICollection<Products> listProductTypes { get; set; }


}
public partial class Products
{
[Key]
public int ProductID { get; set; }
[Display(Name = "محصول")]
public string Title { get; set; }
public int ProductTypeID { get; set; }
public ProductTypes ProductTypes { get; set; }
public int ProductAbilityID { get; set; }
public ProductAbility ProductAbility { get; set; }
}


بعد با دستور add-migration ser1 میگریشن می سازم
یا از ProductAbilityID و یا ProductTypeID
یکی به صورت
ProductAbilityroductAbilityID
یا
ProductTypesProductTypeID
می سازه


کمک کنید

Reza Safa
پنج شنبه 12 فروردین 1400, 16:30 عصر
این نمونه generate هست

migrationBuilder.CreateTable(
name: "products",
columns: table => new
{
ProductID = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
ProductTypeID = table.Column<int>(type: "int", nullable: false),
ProductTypesProductTypeID = table.Column<int>(type: "int", nullable: true),
ProductAbilityID = table.Column<int>(type: "int", nullable: false)
}

DotnetLearn.com
شنبه 14 فروردین 1400, 16:28 عصر
سلام دوست من. وقتتون بخیر.
توصیه میکنم بجای استفاده از دیتاانوتیشین ها از FluentApi استفاده کنید. بسیار انعطاف پذیر هست.

اما مشکلتون:
شما باید کلید خارجی رو توی جداولتون (سمت چند) معرفی کنید

مثال زیر را ببینید:


public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }

public int StandardRefId { get; set; }

[ForeignKey("StandardRefId")]
public Standard Standard { get; set; }
}

public class Standard
{
public int StandardId { get; set; }
public string StandardName { get; set; }

public ICollection<Student> Students { get; set; }
}

آموزش Asp.net Core (https://dotnetlearn.com/fa/Tutorial/%D8%A2%D9%85%D9%88%D8%B2%D8%B4-Asp-net-Core-5-%D8%AF%D8%B1-%D9%82%D8%A7%D9%84%D8%A8-%D9%81%D8%B1%D9%88%D8%B4%DA%AF%D8%A7%D9%87-%D8%A7%DB%8C%D9%86%D8%AA%D8%B1%D9%86%D8%AA%DB%8C) از صفر تا صد بصورت پروژه محور. (فارسی سازی قالب، طراحی دیتابیس و مدیریت سرور و ...)

DotnetLearn.com
شنبه 14 فروردین 1400, 16:28 عصر
لینک :
https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

Reza Safa
شنبه 14 فروردین 1400, 19:18 عصر
ممنونم از پاسختون

بزرگوار دوباره همون فیلد تکرار شد


migrationBuilder.CreateTable(
name: "products",
columns: table => new
{
ProductID = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
ProductTypeID = table.Column<int>(type: "int", nullable: false),
ProductTypesProductTypeID = table.Column<int>(type: "int", nullable: true),
ProductAbilityID = table.Column<int>(type: "int", nullable: false)
},


ProductTypesProductTypeID این فیلد دوباره تکرار شده



public partial class ProductAbility
{
[Key]
public int ProductAbilityID { get; set; }
[Display(Name = "قابلیت")]
public string Ability { get; set; }
[DataType(DataType.MultilineText)]
[Display(Name = "محتوا")]
public String Body { get; set; }
[DataType(DataType.Date)]
[Display(Name = "تاریخ")]
public DateTime ReleaseDate { get; set; }
public ICollection<Products> listProductAbility { get; set; }
}
public partial class ProductTypes
{
[Key]
public int ProductTypeID { get; set; }
[Display(Name = "نوع محصول")]
public string Title { get; set; }


[DataType(DataType.Date)]
[Display(Name = "تاریخ")]
public DateTime ReleaseDate { get; set; }
public ICollection<Products> listProductTypes { get; set; }


}
public partial class Products
{
[Key]
public int ProductID { get; set; }
[Display(Name = "محصول")]
public string Title { get; set; }
[ForeignKey("ProductTypeIDRef")]
public int ProductTypeID { get; set; }
public ProductTypes ProductTypes { get; set; }
[ForeignKey("ProductAbilityIDRef")]
public int ProductAbilityID { get; set; }
public ProductAbility ProductAbility { get; set; }
}