سوال در ارتباط با entity framework .net core ارتباط یک به چند
سلام خدمت همه دوستان
من قبل از اینکه با .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
می سازه
کمک کنید
نقل قول: سوال در ارتباط با entity framework .net core ارتباط یک به چند
این نمونه 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)
}
نقل قول: سوال در ارتباط با entity framework .net core ارتباط یک به چند
سلام دوست من. وقتتون بخیر.
توصیه میکنم بجای استفاده از دیتاانوتیشین ها از 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 از صفر تا صد بصورت پروژه محور. (فارسی سازی قالب، طراحی دیتابیس و مدیریت سرور و ...)
نقل قول: سوال در ارتباط با entity framework .net core ارتباط یک به چند
نقل قول: سوال در ارتباط با entity framework .net core ارتباط یک به چند
ممنونم از پاسختون
بزرگوار دوباره همون فیلد تکرار شد
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; }
}