英文:
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 => . . .;
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论