C# 表中列值中列表的正确用法?
这是我正在处理的JSON文件的结构C# 表中列值中列表的正确用法?,c#,sql,entity-framework,linq,C#,Sql,Entity Framework,Linq,这是我正在处理的JSON文件的结构 { "date": "2015-11-11", "retailer_id": "CLD001", "orders": [ { "products": [ { "product_id": "53743443003", "quantity": 4, "unit_price": 42.71 } ], "value": 1
{
"date": "2015-11-11",
"retailer_id": "CLD001",
"orders": [
{
"products": [
{
"product_id": "53743443003",
"quantity": 4,
"unit_price": 42.71
}
],
"value": 170.84,
"customer": {
"id": 58
}
}
]
}
为了处理这个Json文件,我创建了3个单独的类来包含productsOrdered、OrderItems和RetailerOrders等信息。这是说sql不能将类型与通用系统列表匹配
[Table]
public class OrderItems : INotifyPropertyChanged, INotifyPropertyChanging
{
private List<ProductsOrdered> po = new List<ProductsOrdered>();
private double TotalPrice;
private int customer_id;
///this list is also throwing a error i'd suspect after the parent is fixed
[JsonProperty(PropertyName = "products")]
[Column]
通过一些调查,我认为一旦进入列表,就应该使用[ASSOCIATION]而不是[Column],但我不确定如何正确处理表格的形成,我认为您试图将类重新用于JSON、MVVM和SQL,这是一个令人困惑的问题。对于序列化、视图和实体,最好使用不同(但最终非常相似)的类。删除所有不必要的内容以获得一个属性如何?customerid JsonProperty是id,但在JSON中它位于customer下,然后id位于方括号下,这可能会导致其他问题吗?可以,但这些类是用来创建表的,它说…--什么是“它”?您必须按照上面的建议将其转换为MCVE。由于缩进错误、与问题无关的属性和空白,代码很难阅读。所有这些属性都可能是自动属性。这看起来像是一个注释,而不是一个答案。也许,但我认为这也是问题的答案。它们是Q中的几个级别的问题,最重要的是OP不知道如何写正确的问题。老实说,我现在比我发布问题时更困惑。我认为这可能是因为[Association]关键字,但现在我不知道这是一个案例,你真的需要一个有经验的人,与你坐在一起,与你讨论问题。尝试将类拆分为“反序列化json”、“视图模型和UI通知”以及“数据库实体”。您可以在每个类中添加和删除其他地方不需要的字段(例如,数据库对NotifyPropertyChanged不感兴趣,json类不关心[Table]等)。
public List<ProductsOrdered> Productsordered
{
get { return po; }
set
{
NotifyPropertyChanging("Products Ordered");
po = value;
NotifyPropertyChanged("Products Ordered");
}
}
[JsonProperty(PropertyName = "value")]
[Column]
public double totalprice
{
get { return TotalPrice; }
set {
NotifyPropertyChanging("Total Price");
TotalPrice = value;
NotifyPropertyChanged("Total Price");
}
}
[JsonProperty(PropertyName = "id")]
[Column(IsPrimaryKey = true, IsDbGenerated = false, AutoSync = AutoSync.OnInsert)]
public int customerid
{
get { return customer_id; }
set {
NotifyPropertyChanging("Customer_ID");
customer_id = value;
NotifyPropertyChanged("Custome_ID");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
}
[Table]
public class RetailOrders : INotifyPropertyChanged, INotifyPropertyChanging
{
private List<OrderItems> oi;
private string retailer_id;
private DateTime date;
[Column(IsPrimaryKey = true, IsDbGenerated = false, AutoSync = AutoSync.OnInsert)]
[JsonProperty(PropertyName = "retailer_id")]
public string Retailer_id
{
get { return retailer_id; }
set {
NotifyPropertyChanging("Retailer ID");
retailer_id = value;
NotifyPropertyChanged("Retailer ID");
}
}
[JsonProperty(PropertyName = "orders")]
[Column]
public List<OrderItems> OrderItems
{
get { return oi; }
set {
NotifyPropertyChanging("OrderItems");
oi = value;
NotifyPropertyChanged("OrderItems");
}
}
[JsonProperty(PropertyName = "date")]
[Column]
public DateTime Date
{
get { return date; }
set {
NotifyPropertyChanging("date");
date = value;
NotifyPropertyChanged("date");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
}
[Table]
public class ProductsOrdered : INotifyPropertyChanged, INotifyPropertyChanging
{
private string productID;
private int quantity;
private double unit_price;
[JsonProperty(PropertyName = "product_id")]
[Column(IsPrimaryKey = true, IsDbGenerated = false, AutoSync = AutoSync.OnInsert)]
public string ProductID
{
get { return productID; }
set
{
NotifyPropertyChanging("product ID");
productID = value;
NotifyPropertyChanged("product ID");
}
}
[JsonProperty(PropertyName = "quantity")]
[Column]
public int Quantity
{
get { return quantity; }
set {
NotifyPropertyChanging("quantity");
quantity = value;
NotifyPropertyChanged("quantity");
}
}
[JsonProperty(PropertyName = "unit_price")]
[Column]
public double UnitPrice
{
get { return unit_price; }
set {
NotifyPropertyChanging("unit price");
unit_price = value;
NotifyPropertyChanged("unit price");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
}