如何在 .Net Maui 中的 SQLite 表类定义中创建一个计算列?

huangapple go评论62阅读模式
英文:

How do I create a computed column in an SQLite Table class definition in .Net Maui?

问题

在SQLite Table类定义中创建计算列的方法是:

[SQLite.Table("ItemHist")]
public class cl_ItemHist : TableData
{
    [NotNull, ForeignKey(typeof(cl_Stores))]
    public int Store_id { get; set; }

    [NotNull]
    public DateTime PurchDate { get; set; }

    [NotNull, ForeignKey(typeof(cl_Items))]
    public int Item_id { get; set; }

    [NotNull]
    public float ItemQty { get; set; }

    [NotNull]
    public float TotalPrice { get; set; }

    [ForeignKey(typeof(cl_SaleCodes))]
    public int SaleCode_id { get; set; }

    [NotNull, ForeignKey(typeof(cl_Uom))]
    public int UnitPriceUom_id { get; set; }

    [NotNull, DefaultValue(0)]
    public int Purchased { get; set; }

    // References cl_Stores
    [OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public cl_Stores StoreName { get; set; }

    // References cl_Items
    [OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public cl_Items ItemName { get; set; }

    [OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public cl_Items ItemSize { get; set; }

    // References cl_SaleCodes
    [OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public cl_SaleCodes SaleCode { get; set; }

    // References cl_Uom from cl_Items
    [OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
    public cl_Uom Uom { get; set; }

    // Computed column
    [Ignore] // Ignore this property for SQLite
    public float ComputedColumn
    {
        get { return TotalPrice / (ItemSize * ItemQty); }
    }
}

请注意,我在ComputedColumn属性上添加了[Ignore]属性,以指示SQLite忽略此属性,因为它是一个计算列,而不是数据库中的实际列。这将允许您在应用程序中计算和使用ComputedColumn的值,但不会在SQLite数据库中创建对应的列。

英文:

How do I create a computed column in an SQLite Table class definition in .Net Maui?

Here is the table...

[SQLite.Table("ItemHist")]
public class cl_ItemHist : TableData
{
	[NotNull, ForeignKey(typeof(cl_Stores))]
	public int Store_id { get; set; }
       
	[NotNull]
	public DateTime PurchDate { get; set; }
        
	[NotNull, ForeignKey(typeof(cl_Items))]
	public int Item_id { get; set; }
       
	[NotNull] 
	public float ItemQty { get; set; }
       
	[NotNull]
	public float TotalPrice { get; set; }
       
	[ForeignKey(typeof(cl_SaleCodes))]
	public int SaleCode_id { get; set; }

	[NotNull, ForeignKey(typeof(cl_Uom))]
	public int UnitPriceUom_id { get; set;}
        
	[NotNull, DefaultValue(0)]
	public int Purchased { get; set; }

	// References cl_Stores
	[OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
	public cl_Stores StoreName { get; set; }
        
	// References cl_Items
	[OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
	public cl_Items ItemName { get; set; }
        
	[OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
	public cl_Items ItemSize { get; set; }

	// References cl_SaleCodes
	[OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
	public cl_SaleCodes SaleCode { get; set; }

	// References cl_Uom from cl_Items
	[OneToOne(CascadeOperations = CascadeOperation.CascadeRead)]
	public cl_Uom Uom { get; set; }
}

The computed column I want to include contains three existing columns:

(TotalPrice / (ItemSize * ItemQty))

答案1

得分: 2

"要排除计算属性不被存储到sqlite,请标记为[Ignore]。\ncs\n[Ignore]\npublic float MyComputedColumn => . . .;\n"

"然而,您提到了"dynamic"。如果您需要在三个影响此列的属性任何更改时立即更新显示,您将需要在这三个属性的setter中添加额外的代码。而且,cl_itemHist类需要实现INotifyPropertyChanged。"

英文:

To exclude the computed property from sqlite, mark it [Ignore].

[Ignore]
public float MyComputedColumn => . . .;

https://learn.microsoft.com/en-us/xamarin/android/data-cloud/data-access/using-sqlite-orm#sqlite-attributes

However, you mention "dynamic". If you need display to be updated immediately, on any change of the three properties that affect this column, you’ll need additional code in the setters of those three properties. And class cl_itemHist will need to implement INotifyPropertyChanged.

huangapple
  • 本文由 发表于 2023年5月22日 04:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定