I have used Fluent-API (EF Core in ASP.NET Core MVC) for my project to map code to database in SQL Server, but I am facing foreign key problem:
Please help me!
I have LoaiDichVu class and LoaiDichVuConfiguration class as follows:
public class LoaiDichVu
{
public Guid ID { get; set; }
public string tenLoaiDichVu { get; set; }
public string moTa { get; set; }
public string trangThai { get; set; }
public string fields1 { get; set; }
public string fields2 { get; set; }
public string fields3 { get; set; }
public string fields4 { get; set; }
public string fields5 { get; set; }
public ICollection<DichVu> ICDichVu { get; set; }
}
public class LoaiDichVuConfiguration : IEntityTypeConfiguration<LoaiDichVu>
{
public void Configure(EntityTypeBuilder<LoaiDichVu> builder)
{
builder.ToTable("LoaiDichVu");
builder.HasKey(ldv => ldv.ID);
builder.Property(ldv => ldv.ID).HasDefaultValueSql("newsequentialid()");
builder.Property(ldv => ldv.tenLoaiDichVu).HasMaxLength(30);
builder.Property(ldv => ldv.moTa).HasMaxLength(60);
builder.Property(ldv => ldv.trangThai).HasMaxLength(30);
}
}
And, I have DichVu and DichVuConfiguration class as follows:
public class DichVu
{
public Guid ID { get; set; }
public Guid ID_LoaiDichVu { get; set; }
public LoaiDichVu LoaiDichVu { get; set; }
//Tên dịch vụ: Nước khoáng...
public string tenDichVu { get; set; }
//Đơn vị tính: Chiếc/ cái/ bao...
public string donViTinh { get; set; }
public decimal donGia { get; set; }
//Đơn vị tính: VNĐ
public string donViTien { get; set; }
public string moTa { get; set; }
public string trangThai { get; set; }
public string fields1 { get; set; }
public string fields2 { get; set; }
public string fields3 { get; set; }
public string fields4 { get; set; }
public string fields5 { get; set; }
public ICollection<ChiTiet_HoaDon> ICChiTietHoaDon { get; set; }
}
public class DichVuConfiguration : IEntityTypeConfiguration<DichVu>
{
public void Configure(EntityTypeBuilder<DichVu> builder)
{
builder.ToTable("DichVu");
builder.HasKey(dv => dv.ID);
builder.Property(dv => dv.ID).HasDefaultValueSql("newsequentialid()");
builder.Property(dv => dv.tenDichVu).HasMaxLength(50);
builder.Property(dv => dv.donViTinh).HasMaxLength(20);
builder.Property(dv => dv.donViTien).HasMaxLength(10);
builder.Property(dv => dv.moTa).HasMaxLength(100);
builder.Property(dv => dv.trangThai).HasMaxLength(30);
builder.HasOne(dv => dv.LoaiDichVu).WithMany(dv => dv.ICDichVu).HasForeignKey(dv => dv.ID_LoaiDichVu).OnDelete(DeleteBehavior.NoAction);
}
}
Then, I have a ModelBuilderExtensions class:
public static class ModelBuilderExtensions
{
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<LoaiDichVu>().HasData(
new LoaiDichVu() { ID = Guid.NewGuid(), tenLoaiDichVu = "Eat" }
);
modelBuilder.Entity<DichVu>().HasData(
new DichVu() { ID = Guid.NewGuid(), ID_LoaiDichVu = Guid.NewGuid(),tenDichVu = "Rice", donGia = 99}
);
}
}
I have successfully added a migration.
If I set ID_LoaiDichVu as above, I get the following error:
I don't know how to get ID from LoaiDichVu table to assign to ID_LoaiDichVu in DichVu table
Please direct me how to assign by code instead of copy and assign
Example: LoaiDichVu (ID = 63763eb2-e537-4b97-a394-0529d216aa74)
And then assign
DichVu (ID = Guid.NewGuid(), ID_LoaiDichVu = 63763eb2-e537-4b97-a394-0529d216aa74)
Thank you! :)


