2014-10-19 38 views
0

我有以下实体对象(数据库版本上右):DbUpdateException被抛出了未知的原因

Invoker     
(N) Commands 

Command      
(1) Event    EventId(FKEY) 
(N) Effects    
(N) Conditions 
--Other members 

Effect 
(1) EffectType  EffectTypeId(FKEY) 
(N) Properties  CommandId(FKEY) 
(N) Conditions 
--Other members 

         CommandConditions: ConditionId(PKEY, FKEY), CommandId(FKEY)  
         EffectConditions: ConditionId(PKEY, FKEY), EffectId(FKEY)        

Condition 
(1) ConditionType   
(N) Properties 

         EffectProperties: PropertyId(PKEY, FKEY), EffectId(FKEY) 
         ConditionProperties: PropertyId(PKEY, FKEY), ConditionId(FKEY) 

Property 
Name 
Value 

当我想更新现有调用实体,我不喜欢这样写道:

public void UpdateInvoker(Invoker existingInvoker, InvokerViewModel contract, MyEntities context) 
{ 
    //typeof(Invoker.Command = CommandViewModel 
    existingInvoker.Commands = contract.Commands.Select(c => c.ToDataCommand(context)); 
} 

public Command ToDataCommand(MyEntities context) 
{ 
     var command = context.Commands.Create(); 
     command.CommandEvent = context.Events.First(e => e.EventName == Event.Name); 
     command.CommandConditions = Conditions.Select(c => c.ToDataCondition(context)).ToList(); 
     command.CommandEffects = Effects.Select(e => e.ToDataEffect(context)).ToList(); 
     context.Commands.Add(command); 
     return command; 
} 

用于生成命令的相同设置也用于生成效果,条件和属性实体。当我犯这个交易,我收到一个有点无助DbUpdateException:

型“System.Data.Entity.Infrastructure.DbUpdateException”的第一次机会异常出现在EntityFramework.dll

其他信息:冲突的改变检测。尝试插入具有相同密钥的多个实体时可能会发生这种情况。

但是,这个设置如何创建具有匹配键的实体?当实体在同一列上有PKEY和FKEY时,实体框架是否工作不正常?

回答

0

看来问题在于实体框架不喜欢外键和主键在同一列上。删除这个问题解决了我的问题。

相关问题