2

この Linq クエリに頭を悩ませています。エンティティフレームワークです。クエリは、さまざまなモデル オブジェクトへの変換を行います。エラーの原因として示したコードの領域をコメントまたは削除すると、linq クエリが機能します。しかし、このエラーが発生することなく、このようなクエリを作成できるはずだと私には思えます:

タイプ 'DomainModel.Model.CustomerAddress' の null 定数値を作成できません。このコンテキストでは、エンティティ型、列挙型、またはプリミティブ型のみがサポートされます。

できない理由、またはもっと重要なことに、それを機能させるために何を変更する必要があるかを誰か教えてもらえますか?

ありがとう

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

1 に答える 1

0

わかりました、私はこれを理解しました。問題は、データベースがこれらの関連エンティティへの null forien キーを許可していることです。forien キーが null 可能ではないようにデータベースを変更し、データベースからモデルを更新しました。私は、データを持っていないことだけでなく、なぜ持っていないのかを表すデータへのリンクを持つことができるように、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;
    }

しかもノーエラー!

于 2013-04-13T05:31:27.090 に答える