【问题标题】:Correct usage of a list in a column value in a table?在表中的列值中正确使用列表?
【发布时间】:2019-01-22 13:15:24
【问题描述】:

这是我正在使用的 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;
    }

///这是我的 productsOrdered 表,其中包含一个具有 ID、QUANITY 和 UNIT PRICE 的对象

    [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] 但是我不确定处理表格的正确方法是什么

【问题讨论】:

  • 删除所有不必要的东西来拥有MCVE怎么样?
  • 您的 customerid JsonProperty 是 id,但在 JSON 中它在 customer 下,然后 id 在括号下,会不会导致其他问题?
  • 可以,但是这些类是用来创建表的
  • 它在说... -- 什么是“它”?您必须按照上面的建议将其转换为 MCVE。由于缩进不良、与问题无关的属性和空白,代码很难阅读。所有这些属性都可以是自动属性。

标签: c# sql entity-framework linq


【解决方案1】:

我认为您通过尝试重用 JSON、MVVM 和 SQL 的类来混淆问题。为序列化、视图和实体提供不同(但最终非常相似)的类要好得多。

【讨论】:

  • 这看起来像是评论而不是答案。
  • 也许,但我认为这也是问题的答案。
  • 它们是Q中的几个层次的问题,最上面的一个是OP不知道如何写正确的问题。
  • 说实话,我现在比发布问题时更困惑。我以为可能是因为 [Association] 关键字,但现在我迷路了
  • 这是您真正需要有经验的人与您坐在一起并与您讨论问题的情况之一。尝试将您的类拆分为“反序列化 json”、“查看模型和 UI 通知”以及“数据库实体”。您可以在每个类中添加和删除其他地方不需要的字段(例如,数据库对 NotifyPropertyChanged 不感兴趣,json 类不关心 [Table] 等)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
相关资源
最近更新 更多