关联期间的异常可复用外键 [英] Exception during association fixup with nullable composite foreign keys

查看:136
本文介绍了关联期间的异常可复用外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pocos和可空的外键的问题。
我有2个表(订单和产品)每个表都有一个复合主键(orderid,orderid2)和(productid,productid2)
我已经设置了一个0,1 .. *表。
一个订单可以与0或1个产品相关。
一个产品有与他相关的*订单。



如何崩溃:




  • 使用CreateObject()创建新产品。

  • 将新产品添加到实体集。

  • 创建新订单usung CreateObject ()。

  • 将新订单添加到实体集。



当我添加订单对于产品的订单列表,它会崩溃尝试修复关联(在新订单上设置产品导航属性)

  CREATE TABLE [dbo]。[产品](
[productid] [int] IDENTITY(1,1)NOT NULL,
[productid2] [int] NOT NULL,
[productname] [nchar] (10)NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED

[productid] ASC,
[productid2] ASC
)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]
)ON [PRIMARY]

CREATE TABLE [dbo]。[Orders]
[orderid] [int] NOT NULL,
[orderid2] [int] NOT NULL,
[ordername] [nchar](10)NULL,
[productid] [int ] NULL,
[productid2] [int] NULL,
CONSTRAINT [PK_orders] PRIMARY KEY CLUSTERED

[orderid] ASC,
[orderid2] ASC
)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY]
)ON [PRIMARY]
/ pre>



崩溃代码:

  var product = context.CreateObject< Products>(); 
context.Products.AddObject(product);
var order = context.CreateObject< Orders>();
context.Orders.AddObject(order);

product.Orders.Add(order);
if(order.Product!= product)Console.WriteLine(error);

异常:

 code> System.Data.EntityException未处理
消息=无法在实体类型System.Data.Entity.DynamicProxies.Orders_A0290D8629F0336D278E5AEF2C0F2A91FF56726ED5E3A9FA668AC902696A8651上设置字段/属性Product。有关详细信息,请参阅InnerException。
Source = System.Data.Entity
StackTrace:
在System.Data.Objects.Internal.PocoPropertyAccessorStrategy.SetNavigationPropertyValue(RelatedEnd relatedEnd,Object value)
在System.Data.Objects .Internal.EntityWrapper`1.SetNavigationPropertyValue(RelatedEnd relatedEnd,Object value)
在System.Data.Objects.DataClasses.EntityReference`1.AddToObjectCache(IEntityWrapper wrappedEntity)
在System.Data.Objects.DataClasses。 RelatedEnd.Add(IEntityWrapper wrappedTarget,Boolean applyConstraints,Boolean addRelationshipAsUnchanged,Boolean relationAlreadyExists,Boolean allowModifyingOtherEndOfRelationship,Boolean forceForeignKeyChanges)
在System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedEntity,Boolean applyConstraints)
System.Data.Objects.DataClasses.EntityCollection`1.Add(TEntity entity)
在Proxies.CSharp.Program.Main(String [] args)在Program.cs:行20
在System.Ap pDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的
$ System.Threading.ExecutionContext.Run中的
(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)
在System.Threading中。 ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)
在System.Threading.ThreadHelper.ThreadStart()
InnerException:System.NullReferenceException
Message =对象引用未设置为一个东西。
Source = Proxies.CSharp
StackTrace:
在Proxies.CSharp.Orders.FixupProduct(产品previousValue,Boolean skipKeys)在Orders.cs:行134
在Proxies.CSharp。 Orders.set_Product(Products value)在Orders.cs中:行106
在System.Data.Entity.DynamicProxies.Orders_A0290D8629F0336D278E5AEF2C0F2A91FF56726ED5E3A9FA668AC902696A8651.SetBasePropertyValue(String,Object)
在lambda_method(Closure,Object,String,Object)
在System.Data.Objects.Internal.EntityProxyFactory.TrySetBasePropertyValue(Type proxyType,String propertyName,Object entity,Object value)
在System.Data.Objects.Internal.EntityProxyFactory。  c__DisplayClass8。 < CreateBaseSetter> b__7(Object entity,Object value)
在System.Data.Objects.Internal.PocoPropertyAccessorStrategy.SetNavigationPropertyValue(RelatedEnd relatedEnd,Object value)

注意:它与entytobjects一起工作,它与自我跟踪实体一起工作,如果键不是复合或不可空,它将工作。



我做错了事情或它是真实的bug?

解决方案

我不得不更正poco TT:



行(381,441和475):

 <#= code.Escape(dependentProperty)#> =<#= code.Escape(navProperty)#>。<#= code.Escape(principalProperty)#> ;; 

按行:

 <#= code.FieldName(dependentProperty)#> =<#= code.Escape(navProperty)#>。<#= code.Escape(principalProperty)#> ;; 


I have a problem with Pocos and nullable foreign keys . I have 2 tables (orders and products) each table have a composite primary key (orderid,orderid2) and (productid,productid2) And I have set a 0,1..* association between the two tables. One order can be related to 0 or 1 product. And one product has * orders related to him.

How to crash :

  • Create a new product using CreateObject().
  • Add the new product to then entityset.
  • Create a new order usung CreateObject().
  • Add the new Order to the entityset.

When I add an order to the product's orders list, it crashes trying to fixup the association (setting the product navigation property on the new order)

CREATE TABLE [dbo].[Products](
    [productid] [int] IDENTITY(1,1) NOT NULL,
    [productid2] [int] NOT NULL,
    [productname] [nchar](10) NULL,
 CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
    [productid] ASC,
    [productid2] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[Orders](
    [orderid] [int] NOT NULL,
    [orderid2] [int] NOT NULL,
    [ordername] [nchar](10) NULL,
    [productid] [int] NULL,
    [productid2] [int] NULL,
 CONSTRAINT [PK_orders] PRIMARY KEY CLUSTERED 
(
    [orderid] ASC,
    [orderid2] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Code to crash :

            var product = context.CreateObject<Products>();
            context.Products.AddObject(product);
            var order = context.CreateObject<Orders>();
            context.Orders.AddObject(order);

            product.Orders.Add(order);
            if (order.Product != product) Console.WriteLine("error");

Exception :

System.Data.EntityException was unhandled
  Message=Unable to set field/property Product on entity type System.Data.Entity.DynamicProxies.Orders_A0290D8629F0336D278E5AEF2C0F2A91FF56726ED5E3A9FA668AC902696A8651. See InnerException for details.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.SetNavigationPropertyValue(RelatedEnd relatedEnd, Object value)
       at System.Data.Objects.Internal.EntityWrapper`1.SetNavigationPropertyValue(RelatedEnd relatedEnd, Object value)
       at System.Data.Objects.DataClasses.EntityReference`1.AddToObjectCache(IEntityWrapper wrappedEntity)
       at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedTarget, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists, Boolean allowModifyingOtherEndOfRelationship, Boolean forceForeignKeyChanges)
       at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedEntity, Boolean applyConstraints)
       at System.Data.Objects.DataClasses.EntityCollection`1.Add(TEntity entity)
       at Proxies.CSharp.Program.Main(String[] args) in Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=Proxies.CSharp
       StackTrace:
            at Proxies.CSharp.Orders.FixupProduct(Products previousValue, Boolean skipKeys) in Orders.cs:line 134
            at Proxies.CSharp.Orders.set_Product(Products value) in Orders.cs:line 106
            at System.Data.Entity.DynamicProxies.Orders_A0290D8629F0336D278E5AEF2C0F2A91FF56726ED5E3A9FA668AC902696A8651.SetBasePropertyValue(String , Object )
            at lambda_method(Closure , Object , String , Object )
            at System.Data.Objects.Internal.EntityProxyFactory.TrySetBasePropertyValue(Type proxyType, String propertyName, Object entity, Object value)
            at System.Data.Objects.Internal.EntityProxyFactory.<>c__DisplayClass8.<CreateBaseSetter>b__7(Object entity, Object value)
            at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.SetNavigationPropertyValue(RelatedEnd relatedEnd, Object value)

Note : It works with entytobjects, it works with self-tracking entities, and It works if the key is not composite or not nullable.

Am I doing something wrong or it it a real bug ?

解决方案

I had to correct the poco TT :

Chage the lines (381,441 and 475) :

<#=code.Escape(dependentProperty)#> = <#=code.Escape(navProperty)#>.<#=code.Escape(principalProperty)#>;

by the line :

<#=code.FieldName(dependentProperty)#> = <#=code.Escape(navProperty)#>.<#=code.Escape(principalProperty)#>;

这篇关于关联期间的异常可复用外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆