因未知原因而抛出DbUpdateException

时间:2014-10-19 23:08:15

标签: entity-framework

我有以下实体对象(右边的数据库版本):

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:

  

EntityFramework.dll中出现'System.Data.Entity.Infrastructure.DbUpdateException'类型的第一次机会异常

     

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

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

1 个答案:

答案 0 :(得分:0)

似乎问题在于实体框架不喜欢外键而主键在同一列上。删除此问题解决了我的问题。

相关问题