c# - 正确使用表中列值中的列表?

标签 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": 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]

///设置已订购的产品列表

 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;
    }

///错误源自的零售商订单 System.Data.Linq.dll 中发生类型为“System.InvalidOperationException”的未处理异常

  [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");
                }
        }

有关该错误的更多信息表明它起源于此处,无法确定“System.Collections.Generic.List`1[SalesManagement.OrderItems]”的 SQL 类型。这让我觉得我错误地设置了列值

        [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;
    }

///这是我的产品订购表,其中包含一个具有 ID、质量和单价的对象

    [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;
    }

通过一些调查,它让我相信一旦进入列表就使用 [ASSOCIATION] 而不是 [Column] 但是我不确定处理表格的正确方法是什么

最佳答案

我认为您在尝试重用 JSON、MVVM 和 SQL 的类时混淆了问题。为序列化、 View 和实体设置不同(但最终非常相似)的类要好得多。

关于c# - 正确使用表中列值中的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54309142/

相关文章:

c# - 在 C# 中重载复合赋值运算符的简单方法?

c# - 操作方法 : Stop adding files to "Recent files" in Quick Access when opening/saving files with Microsoft. Office.Interop

c# - 为什么 WPF 数据绑定(bind)会吞下异常?

C# 删除空 url 参数正则表达式

mysql - INSERT 后触发删除空行

php - 使用 LIKE 子句搜索 Mysql

sql - 限制类别页面上产品列表中子类别的项目数量?

c# - Entity Framework 代码优先数据库不更新

linq - Entity Framework /Linq - groupby 和having 子句

c# - 计算存储过程的结果