簡體   English   中英

使用 ef core 3.1 添加來自不同表的計算列

[英]Add calculated column from different tables usin ef core 3.1

如果相關表中的另一列發生變化,我想計算列值。

Product class:

public class Product
{
    private int totalQuantity;

    private bool quantityAlert;

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ProductId { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int MinimumQuantity { get; set; }
    public string Description { get; set; }
    public string BarCode { get; set; }
    public string ProductCode { get; set; }
    public int CategoryId { get; set; }
    public int UnitId { get; set; }
    public virtual Unit Unit { get; set; }
    public virtual Category Category { get; set; }
    public virtual List<StockProduct> StockProducts { get; set; }

    public int TotalQuantity
    {
        get
        {
            return totalQuantity;
        }
        protected set
        {
            int q = 0;

            foreach (var product in StockProducts)
            {
                q += product.Quantity;
            }
            totalQuantity = q;
        }
    }

    public bool QuantityAlert
    {
        get { return quantityAlert; }
        protected set
        { 
            quantityAlert = TotalQuantity <= MinimumQuantity; 
        }
    }
}

庫存產品StockProduct

public class StockProduct
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid StockProductId { get; set; }
    [Required]
    public int Quantity { get; set; }
    [Required]
    public double BuyingPrice { get; set; }
    public double SellingPrice { get; set; }
    public Guid ProductId { get; set; }
    public virtual Product Product { get; set; }
}

appContext配置:

modelBuilder.Entity<Product>()
    .Property(p => p.TotalQuantity)
    .ValueGeneratedOnAddOrUpdate()
    .UsePropertyAccessMode(PropertyAccessMode.Property);

modelBuilder.Entity<Product>()
    .Property(p => p.QuantityAlert)
    .ValueGeneratedOnAddOrUpdate()
    .UsePropertyAccessMode(PropertyAccessMode.Property);

錯誤信息是

SQLite 錯誤 19:“NOT NULL 約束失敗:Products.TotalQuantity”。

如果我為總數量和最小數量設置默認值,我會得到同樣的錯誤,但如果QuantityAlert的默認值設置為 false,即使默認總數量 = 0 小於默認最小數量 = 0,它也會保持 false。

不根據

quantityAlert = TotalQuantity <= MinimumQuantity; 

Entity Framework Core Fluent API ValueGeneratedOnAddOrUpdate 提供了一種方法來指示無論何時將新實體添加到數據庫或修改現有實體,都會生成所選屬性的值。 因此,當 EF Core 生成 SQL 時,該屬性不應包含在 INSERT 或 UPDATE 語句中。 這意味着它在您的數據庫中有一個默認列值,或者它將由觸發器生成。 正如我猜你沒有任何這些所以你必須從你的數據庫上下文中刪除 ValueGeneratedOnAddOrUpdate 。 並更改您的 TotalQuantity 和 QuantityAlert 屬性

 public int TotalQuantity
    {
        get { return  StockProducts==null? 0: StockProducts.Sum(p=>p.Quantity; }
        protected set {}
}
    }
public bool QuantityAlert
    {
        
         get {return   quantityAlert = TotalQuantity <= MinimumQuantity; }
       protected set {}
}
    }

並且不要忘記在您的任何 select 產品查詢中包含 StockProducts。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM