c# - Linq to EF 系统不支持异常 System.NotSupportedException

标签 c# .net linq entity-framework linq-to-entities

为这个 Linq 查询抓狂。它是 Entity Framework 。查询确实转换为不同的模型对象。如果我评论或删除我指出导致错误的代码区域,则 linq 查询有效。但在我看来,我应该能够编写这样的查询而不会出现此错误:

无法创建“DomainModel.Model.CustomerAddress”类型的空常量值。在此上下文中仅支持实体类型、枚举类型或原始类型。

谁能告诉我为什么我不能或更重要的是我需要改变什么才能让它发挥作用?

谢谢

var orders = from o in _db.Orders
                          .Include("User")
                          .Include("OrderItems.Inventories.Product")
                          .Include("Transactions")
                          .Include("CustomerAddressBilling.Address")
                          .Include("CustomerAddressBilling.Customer")
                          .Include("CustomerAddressBilling.Contact")
                          .Include("CustomerAddressShipping.Address")
                          .Include("CustomerAddressShipping.Customer")
                          .Include("CustomerAddressShipping.Contact")
                          .Include("ShippingMethod")
                     // this works ok
                         let items = (o.OrderItems.Select(i => new Model.OrderItem
                         {
                             OrderItemId = i.OrderItemId,
                             OrderId = i.OrderId,
                             DateAdded = i.CreateDate,
                             LineItemPrice = i.Inventories.Sum(ii => ii.Product.Price),
                             Product = i.Inventories.FirstOrDefault().Product,
                             Quantity = i.Inventories.Count()
                         }))
                     // this works ok
                     let transactions = (o.Transactions.Select(t => new Model.Transaction
                         {
                             Id = t.TransactionId,
                             OrderId = t.OrderId,
                             Amount = t.Amount,
                             AuthorizationCode = t.AuthorizationCode,
                             DateExecuted = t.TransactionDate,
                             Notes = t.Notes,
                             Processor = (Model.TransactionProcessor)t.ProcessorId
                         }))
                     // this causes the error
                     let cab = o.CustAddBillingId.HasValue ? (new Model.CustomerAddress
                         {
                             Id = o.CustomerAddressBilling.CustAddId,
                             UserName = o.User.UserName,
                             FirstName = o.CustomerAddressBilling.Customer.FirstName,
                             LastName = o.CustomerAddressBilling.Customer.LastName,
                             MiddleInitial = o.CustomerAddressBilling.Customer.MiddleName,
                             Salutation = o.CustomerAddressBilling.Customer.Salutation,
                             Suffix = o.CustomerAddressBilling.Customer.Suffix,
                             Street1 = o.CustomerAddressBilling.Address.Line1,
                             Street2 = o.CustomerAddressBilling.Address.Line2,
                             Street3 = o.CustomerAddressBilling.Address.Line3,
                             City = o.CustomerAddressBilling.Address.City,
                             StateOrProvince = o.CustomerAddressBilling.Address.State,
                             Zip = o.CustomerAddressBilling.Address.PostalCode,
                             Country = o.CustomerAddressBilling.Address.Country,
                             Latitude = o.CustomerAddressBilling.Address.Lat,
                             Longitude = o.CustomerAddressBilling.Address.Long,
                             Email = o.CustomerAddressBilling.Contact.ContactInfo,
                             IsDefault = o.CustomerAddressBilling.IsPrimary
                         }) : default(Model.CustomerAddress)
                     // this causes the error
                     let cas = o.CustAddShippingId.HasValue ? (new Model.CustomerAddress
                         {
                             Id = o.CustomerAddressShipping.CustAddId,
                             UserName = o.User.UserName,
                             FirstName = o.CustomerAddressShipping.Customer.FirstName,
                             LastName = o.CustomerAddressShipping.Customer.LastName,
                             MiddleInitial = o.CustomerAddressShipping.Customer.MiddleName,
                             Salutation = o.CustomerAddressShipping.Customer.Salutation,
                             Suffix = o.CustomerAddressShipping.Customer.Suffix,
                             Street1 = o.CustomerAddressShipping.Address.Line1,
                             Street2 = o.CustomerAddressShipping.Address.Line2,
                             Street3 = o.CustomerAddressShipping.Address.Line3,
                             City = o.CustomerAddressShipping.Address.City,
                             StateOrProvince = o.CustomerAddressShipping.Address.State,
                             Zip = o.CustomerAddressShipping.Address.PostalCode,
                             Country = o.CustomerAddressShipping.Address.Country,
                             Latitude = o.CustomerAddressShipping.Address.Lat,
                             Longitude = o.CustomerAddressShipping.Address.Long,
                             Email = o.CustomerAddressShipping.Contact.ContactInfo,
                             IsDefault = o.CustomerAddressShipping.IsPrimary
                         }) : default(Model.CustomerAddress)
                     // this causes the error
                     let sm = o.ShippingMethodId.HasValue ? (new ShippingMethod
                             {
                                 Id = o.ShippingMethod.ShippingMethodId,
                                 Carrier = o.ShippingMethod.Carrier,
                                 ServiceName = o.ShippingMethod.ServiceName,
                                 BaseRate = o.ShippingMethod.BaseRate,
                                 RatePerPound = o.ShippingMethod.RatePerPound,
                                 DaysToDeliver = o.ShippingMethod.DaysToDeliver,
                                 EstimatedDelivery = o.ShippingMethod.EstimatedDelivery
                             }) : default(ShippingMethod)
                     select new Model.Order
                         {
                             Id = o.OrderId,
                             UserName = o.User.UserName,
                             DateCreated = o.CreateDate,
                             Items = items.AsQueryable(),
                             Transactions = transactions.AsQueryable(),
                             ShippingAddressId = o.CustAddShippingId,
                             BillingAddressId = o.CustAddBillingId,
                             // have to comment these next 3 lines as well
                             ShippingAddress = cas,
                             BillingAddress = cab,
                             ShippingMethod = sm,
                             // to here
                             UserLanguageCode = "en",
                             DateShipped = o.ShippedDate,
                             EstimatedDelivery = o.EstimatedDelivery,
                             TrackingNumber = o.TrackingNumber,
                             TaxAmount = o.TaxAmount,
                             DiscountReason = o.DiscountReason,
                             DiscountAmount = o.DiscountAmount
                         };

最佳答案

好的,我已经弄明白了。问题是数据库允许那些相关实体的空外键,我更改了数据库,因此外键不可为空,并从数据库更新了模型。我创建了代表非 ShippingMethods 电子和面对面的新数据实体,并且还有面对面的 CustomerAddress,因此我可以链接到数据,这些数据不仅代表我没有数据,还代表我为什么没有数据。

    public IQueryable<Model.Order> GetOrders()
    {
        var orders = from o in _db.Orders
                                  .Include("User")
                                  .Include("OrderItems.Inventories.Product")
                                  .Include("Transactions")
                                  .Include("CustomerAddressBilling.Address")
                                  .Include("CustomerAddressBilling.Customer")
                                  .Include("CustomerAddressBilling.Contact")
                                  .Include("CustomerAddressShipping.Address")
                                  .Include("CustomerAddressShipping.Customer")
                                  .Include("CustomerAddressShipping.Contact")
                                  .Include("ShippingMethod")
                     let items = (o.OrderItems.Select(i => new Model.OrderItem
                         {
                             OrderItemId = i.OrderItemId,
                             OrderId = i.OrderId,
                             DateAdded = i.CreateDate,
                             LineItemPrice = i.Inventories.Sum(ii => ii.Product.Price),
                             Product = i.Inventories.FirstOrDefault().Product,
                             Quantity = i.Inventories.Count()
                         }))
                     let transactions = (o.Transactions.Select(t => new Model.Transaction
                         {
                             Id = t.TransactionId,
                             OrderId = t.OrderId,
                             Amount = t.Amount,
                             AuthorizationCode = t.AuthorizationCode,
                             DateExecuted = t.TransactionDate,
                             Notes = t.Notes,
                             Processor = (Model.TransactionProcessor)t.ProcessorId
                         }))
                     let cab = new Model.CustomerAddress
                         {
                             Id = o.CustomerAddressBilling.CustAddId,
                             UserName = o.User.UserName,
                             FirstName = o.CustomerAddressBilling.Customer.FirstName,
                             LastName = o.CustomerAddressBilling.Customer.LastName,
                             MiddleInitial = o.CustomerAddressBilling.Customer.MiddleName,
                             Salutation = o.CustomerAddressBilling.Customer.Salutation,
                             Suffix = o.CustomerAddressBilling.Customer.Suffix,
                             Street1 = o.CustomerAddressBilling.Address.Line1,
                             Street2 = o.CustomerAddressBilling.Address.Line2,
                             Street3 = o.CustomerAddressBilling.Address.Line3,
                             City = o.CustomerAddressBilling.Address.City,
                             StateOrProvince = o.CustomerAddressBilling.Address.State,
                             Zip = o.CustomerAddressBilling.Address.PostalCode,
                             Country = o.CustomerAddressBilling.Address.Country,
                             Latitude = o.CustomerAddressBilling.Address.Lat,
                             Longitude = o.CustomerAddressBilling.Address.Long,
                             Email = o.CustomerAddressBilling.Contact.ContactInfo,
                             IsDefault = o.CustomerAddressBilling.IsPrimary
                         }
                     let cas = new Model.CustomerAddress
                         {
                             Id = o.CustomerAddressShipping.CustAddId,
                             UserName = o.User.UserName,
                             FirstName = o.CustomerAddressShipping.Customer.FirstName,
                             LastName = o.CustomerAddressShipping.Customer.LastName,
                             MiddleInitial = o.CustomerAddressShipping.Customer.MiddleName,
                             Salutation = o.CustomerAddressShipping.Customer.Salutation,
                             Suffix = o.CustomerAddressShipping.Customer.Suffix,
                             Street1 = o.CustomerAddressShipping.Address.Line1,
                             Street2 = o.CustomerAddressShipping.Address.Line2,
                             Street3 = o.CustomerAddressShipping.Address.Line3,
                             City = o.CustomerAddressShipping.Address.City,
                             StateOrProvince = o.CustomerAddressShipping.Address.State,
                             Zip = o.CustomerAddressShipping.Address.PostalCode,
                             Country = o.CustomerAddressShipping.Address.Country,
                             Latitude = o.CustomerAddressShipping.Address.Lat,
                             Longitude = o.CustomerAddressShipping.Address.Long,
                             Email = o.CustomerAddressShipping.Contact.ContactInfo,
                             IsDefault = o.CustomerAddressShipping.IsPrimary
                         }
                     let sm = new ShippingMethod
                        {
                            Id = o.ShippingMethod.ShippingMethodId,
                            Carrier = o.ShippingMethod.Carrier,
                            ServiceName = o.ShippingMethod.ServiceName,
                            BaseRate = o.ShippingMethod.BaseRate,
                            RatePerPound = o.ShippingMethod.RatePerPound,
                            DaysToDeliver = o.ShippingMethod.DaysToDeliver,
                            EstimatedDelivery = o.ShippingMethod.EstimatedDelivery
                        }
                     select new Model.Order
                         {
                             Id = o.OrderId,
                             UserName = o.User.UserName,
                             DateCreated = o.CreateDate,
                             Items = items.AsQueryable(),
                             Transactions = transactions.AsQueryable(),
                             ShippingAddressId = o.CustAddShippingId,
                             BillingAddressId = o.CustAddBillingId,
                             ShippingAddress = cas,
                             BillingAddress = cab,
                             ShippingMethod = sm,
                             UserLanguageCode = "en",
                             DateShipped = o.ShippedDate,
                             EstimatedDelivery = o.EstimatedDelivery,
                             TrackingNumber = o.TrackingNumber,
                             TaxAmount = o.TaxAmount,
                             DiscountReason = o.DiscountReason,
                             DiscountAmount = o.DiscountAmount
                         };
        return orders;
    }

没有错误!

关于c# - Linq to EF 系统不支持异常 System.NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875339/

相关文章:

c# - 设置环境变量以在运行时使用 C# 查找 dll

c# - 确定没有第三方组件的音频文件的持续时间

c# - 通过 NetworkStream 从 ZlibStream 读取在 C# .Net 中出现阻塞问题

c# - 为什么我的进程的 Exited 方法没有被调用?

linq - LinqPad 的 lambda 窗口有什么用?

C# 如何使用应用程序配置文件重定向程序集加载

.net - 为什么 dotnet 的 char.IsLower() 是静态方法?

c# - IPAddress 上的奇怪 .Net 行为等于

c# - GroupBy 和 Count by condition Entity Framework

c# - 如何按角色的排序顺序对员工列表进行排序?