简体   繁体   English

Linq to EF系统不支持的异常System.NotSupportedException

[英]Linq to EF system not supported exception System.NotSupportedException

Scratching my head over this Linq query. 我为这个Linq查询抓狂了。 It's Entity Framework. 它是实体框架。 The query does transformation into different model objects. 该查询确实转换为不同的模型对象。 If I comment or remove the areas of the code that I have indicated as causing the error the linq query works. 如果我注释或删除了指示为导致错误的代码区域,则linq查询有效。 But is seems to me that I should be able to write a query like this with out getting this error: 但是在我看来,我应该能够编写这样的查询而不会出现此错误:

Unable to create a null constant value of type 'DomainModel.Model.CustomerAddress'. 无法创建类型为'DomainModel.Model.CustomerAddress'的空常量值。 Only entity types, enumeration types or primitive types are supported in this context. 在此上下文中仅支持实体类型,枚举类型或原始类型。

Can anyone tell me why I cannot or more importantly what I need to change to make it work? 谁能告诉我为什么我不能或更重要地需要进行更改以使其正常工作?

Thanks 谢谢

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

Ok, I have figured this out. 好的,我知道了。 The problem is the database allows null forien keys to those related entities, I changed the database so the forien keys were not nullable and updated the model from the database. 问题是数据库允许对那些相关实体使用空的Forien键,我更改了数据库,以使Forien键不可为空,并从数据库更新了模型。 I created new data enties that represented non-ShippingMethods electronic, and in-person, and also have in-person CustomerAddress so I can have links to data that represents not only that I don't have the data but why I don't have the data. 我创建了新的数据实体,这些实体代表非电子方式的和面对面的非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;
    }

And no errors! 而且没有错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 linq“System.NotSupportedException”出错 - There is an error with linq “System.NotSupportedException” System.NotSupportedException:&#39;LINQ to Entities中不支持指定的类型成员&#39;StartDateTime&#39; - System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities LINQ错误:GroupBy选择上出现System.NotSupportedException - LINQ error: System.NotSupportedException on GroupBy Selection System.NotSupportedException使用DateTime吗? 在Linq to实体 - System.NotSupportedException using DateTime? in Linq to Entities LINQ GroupBy子句System.NotSupportedException错误 - LINQ GroupBy Clause System.NotSupportedException error LINQ嵌套查询问题-System.NotSupportedException - LINQ nested query issue - System.NotSupportedException System.NotSupportedException: &#39;LINQ to Entities 不支持 LINQ 表达式节点类型&#39;Invoke&#39;。&#39; - System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.' 发生System.NotSupportedException - System.NotSupportedException occuring WebRequest System.NotSupportedException - WebRequest System.NotSupportedException System.NotSupportedException:不支持“System.DateOnly”实例的序列化和反序列化 - System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM