我正在開發我的網路應用程式。我使用 .NET 和 EntityFrameworkCore。我已經創建了遷移和資料庫。只是我只想將專案添加到資料庫中,但在向該特定端點發送請求時遇到錯誤。我在下面附上了我的一些代碼。我還將鏈接附加到我的 github 存盤庫https://github.com/szymi-dev/TestRepo
[HttpPost("add-product")]
public async Task<ActionResult<Product>> AddProduct(ProductDto productDto)
{
var product = new Product
{
Name = productDto.Name,
Price = productDto.Price,
Descripiton = productDto.Descripiton,
PictureUrl = productDto.PictureUrl
};
_context.Products.Add(product);
await _context.SaveChangesAsync();
return product;
}
這是productDTO類
public class ProductDto
{
public string Name { get; set; }
public decimal Price { get; set; }
public string Descripiton { get; set; }
public string PictureUrl { get; set; }
}
我還添加了一些物體配置
public class ProductConfiguration : IEntityTypeConfiguration<Product>
{
public void Configure(EntityTypeBuilder<Product> builder)
{
builder.Property(p => p.Id).IsRequired();
builder.Property(p => p.Name).IsRequired().HasMaxLength(50);
builder.Property(p => p.PictureUrl).IsRequired();
builder.Property(p => p.Descripiton).HasMaxLength(180).IsRequired();
builder.Property(p => p.Price).HasColumnType("decimal(18, 2)");
builder.HasOne(p => p.ProductBrand).WithMany().HasForeignKey(k => k.ProductBrandId);
builder.HasOne(p => p.ProductType).WithMany().HasForeignKey(k => k.ProductTypeId);
builder.HasOne(p => p.User).WithMany(p => p.Products).HasForeignKey(k => k.UserId);
}
}
最后我在 Postman 和
Microsoft.EntityFrameworkCore.Database.Command[20102] 執行 DbCommand (6ms) 失敗 [Parameters=[@p0='?' (大小 = 14),@p1='?' (大小 = 6),@p2='?' (大小 = 5), @p3='?', @p4='?', @p5='?', @p6='?'], CommandType='Text', CommandTimeout='30'] INSERT INTO "產品”(“描述”、“名稱”、“PictureUrl”、“價格”、“ProductBrandId”、“ProductTypeId”、“UserId”)值(@p0、@p1、@p2、@p3、@p4、@p5 , @p6); 從“產品”中選擇“Id”,其中更改() = 1 和“rowid” = last_insert_rowid();?在VScode中
uj5u.com熱心網友回復:
我在您的示例和架構中看到了幾個問題。首先,如前所述,您的 Product 具有您需要設定的 ProductType 和 ProductBrand 參考。通常,在您的 UI 中,您將有一個下拉串列,其中包含可供選擇的型別和品牌,因此您的產品 DTO 需要 ProductTypeId 和 ProductBrandId 來表示這些選擇。
物體可以公開一個 FK(您的物體似乎確實具有基于該HasForeignKey屬性的 FK,因此您可以在創建產品時設定這些。否則通常最好設定導航屬性參考,因為這可以驗證提供的 ID 實際上是有效的產品型別和品牌:
var productType = _context.ProductTypes.Single(x => x.ProductTypeId == productDto.ProductTypeId);
var productBrand = _context.ProductBrands.Single(x => x.ProductBrandId == productDto.ProductBrandId);
var product = new Product
{
Name = productDto.Name,
Price = productDto.Price,
Descripiton = productDto.Descripiton,
PictureUrl = productDto.PictureUrl,
ProductType = productType,
ProductBrand = productBrand
};
我通常不建議在您具有導航屬性時公開 FK 屬性,因為這會形成關系的 2 個真實來源。這通常只是更新時的問題,而不是創建時的問題。
最后,您有一個用戶參考,我認為這可能有點問題,具體取決于這種關系的實際用途。一個常見的情況是跟蹤 CreatedBy 型別的關系,盡管這將是一個多對一的場景,用戶不會費心維護與他們創建的產品(和其他物體)的關系。您的 User 定義了 Products 集合,因此我將仔細研究這種關系應該是什么。一個產品在用戶下的關系中與單個用戶相關聯似乎很奇怪。在我想將用戶與這樣的產品相關聯的地方,我通常希望看到一個多對多的關系,其中一個用戶有與之關聯的產品,但這些產品可能與許多用戶相關聯。
就目前而言,您的產品具有必需的用戶參考,因此如果需要將其關聯到當前用戶,那么我將從您的身份驗證狀態或會話狀態中獲取當前用戶 ID,并將其分配給創建時的產品。再次設定 FK 或加載用戶物體并設定用戶導航屬性
uj5u.com熱心網友回復:
您應該使用現有(或新添加的)值填充 Product 物件中的 ProductBrandId、ProductTypeId 和 UserId 欄位,以避免違反外鍵約束。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/464425.html
