我有 3 個模型和一個 User 類,它定義了客戶和賣家的公共屬性,所以我不會附加它(我想這將是無用的資訊)。
public class Seller : User
{
public bool IsApproved { get; set; }
public ICollection<Product> WaitingForShipping { get; set; }
public ICollection<Product> ProductsOnSell { get; set; }
}
public class Customer : User
{
public bool IsSubscriber { get; set; }
public ICollection<Product> WaitingForReceiving { get; set; }
public ICollection<Product> BoughtProducts { get; set; }
}
public class Product
{
[Key] public Guid Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public ProductCategory Category { get; set; }
public int SellerId { get; set; }
[ForeignKey("SellerId")] public Seller ProductSeller { get; set; }
public int CustomerId { get; set; }
[ForeignKey("CustomerId")] public Customer ProductCustomer { get; set; }
public ICollection<Characteristic> ProductInfo { get; set; }
public ICollection<Image> ImagesURLs { get; set; }
public long Quantity { get; set; }
public long AmountSold { get; set; }
public byte Rating { get; set; }
public bool InStock { get; set; }
}
最后是 DbContext 類
public class AppDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Seller> Sellers { get; set; }
public DbSet<Product> Products { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// There were a lot of useless tries of configuring the relationships like
// this
// modelBuilder.Entity<Customer>().HasMany(prods => prods.BoughtProducts)
// .WithOne(s => s.ProductSeller).HasForeignKey(i => i.SellerId);
base.OnModelCreating(modelBuilder);
}
}
當我使用:
dotnet ef migrations add inital
進入終端我得到這個:
無法確定“ICollection”型別的導航“Customer.BoughtProducts”表示的關系。手動配置關系,或使用“[NotMapped]”屬性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此屬性。
我不知道如何解決它。
uj5u.com熱心網友回復:
我不知道您的客戶代碼。但我想你可能會使用 ICollection<Product> 不止一個。以賣家為例。
根據您的 Seller.cs,我在產品中添加以下代碼,
public Guid SellerId { get; set; }
public Seller ProductSeller { get; set; }
public Guid OnSellId { get; set; }
public Seller OnSell { get; set; }
并添加.OnDelete(DeleteBehavior.Restrict);背景關系
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasOne(p => p.ProductSeller)
.WithMany(t => t.WaitingForShipping)
.HasForeignKey(m => m.SellerId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Product>()
.HasOne(p => p.OnSell)
.WithMany(t => t.ProductsOnSell)
.HasForeignKey(m => m.OnSellId)
.OnDelete(DeleteBehavior.Restrict);
}
它有效,我的 customer.cs 在下面。如果您的 Customer.cs 喜歡賣家,請像我一樣更改 Product 和 Context 中的代碼。
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public ICollection<Product> BoughtProducts { get; set; }
}
客戶更新
在背景關系中添加:
modelBuilder.Entity<Product>()
.HasOne(p => p.ProductCustomer)
.WithMany(t => t.WaitingForReceiving)
.HasForeignKey(m => m.CustomerId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Product>()
.HasOne(p => p.BoughtCustomer)
.WithMany(t => t.BoughtProducts)
.HasForeignKey(m => m.BoughtId)
.OnDelete(DeleteBehavior.Restrict);
添加產品:
public Guid CustomerId { get; set; }
public Customer ProductCustomer { get; set; }
public Guid BoughtId { get; set; }
public Customer BoughtCustomer { get; set; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/475992.html
標籤:sql服务器 asp.net 核心 asp.net-core-webapi 数据库关系
上一篇:即使未訪問,EFCore中的延遲加載也會加載所有資料
下一篇:授權過濾測驗失敗webapi
