实体框架代码首先 - 不能在对象'dbo.T_CRProviders'中插入重复键 [英] Entity Framework Code First - Cannot insert duplicate key in object 'dbo.T_CRProviders'

查看:145
本文介绍了实体框架代码首先 - 不能在对象'dbo.T_CRProviders'中插入重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些紧急的问题,我无法在网路上找到答案。



我正在使用CodeFirst EF 4.3.1,我收到一个错误:
违反PRIMARY KEY约束PK_T_CRProviders。不能在对象'dbo.T_CRProviders'中插入重复键。



我的代码是:



模型:

  public enum CRProviderEnums 
{
PE_Abcd = 0,
PE_Efgh
}

[表(T_CRProviders)]
public class CRProvider
{
[Key]
[必需]
public int Enum {get;组;
[必需]
public string Name {get;组; }
}

[表(T_CRSupportedResources)]
public class CRSupportedResource
{
[Key]
public Guid SupportedResourceId {get ;组; }
[必需]
public CRProvider Provider {get;组; }
}

DbContext:

  public class RSContext:DbContext 
{
public DbSet< CRProvider> CRProviders {get;组; }
public DbSet< CRSupportedResource> CRSupportedResources {get;组; }
}

表T_CRProviders如下所示:枚举(PK ),名称



表T_CRSupportedResources如下所示: SupportedResourceId(PK),Provider_Enum(FK)。



在数据库表T_CRProviders中,我已经有一个具有以下值的提供程序:

 枚举:0(这是PE_Abcd)
名称:PE_Abcd

现在我的main()调用一个AddSupportedResource方法。此方法将表T_CRSupportedResources添加到引用提供程序0(PE_Abcd)的新CRSupportedResource。该方法如下所示:

  public void AddSupportedResource()
{
CRSupportedResource supportedResource = new CRSupportedResource )
{
SupportedResourceId = Guid.NewGuid(),
Provider = new CRProvider()
{
枚举=(int)CRProviderEnums.PE_Abcd,
Name =PE_Abcd
}
};

using(RSContext myContext = new RSContext())
{
myContext.CRSupportedResources.Add(supportedResource);

myContext.SaveChanges();
}
}

我希望这种方法会让表T_CRProviders保持不变,并向表T_CRSupportedResources添加一行,如下所示:

  SupportedResourceId:DE532083-68CF-484A-8D2B-606BC238AB61 
Provider_Enum(FK):0(这是PE_Abcd)。

相对于SaveChanges,Entity框架还尝试将提供者添加到T_CRProviders表中,提供者已经存在,它引发以下异常:

 更新条目时出错。 

违反PRIMARY KEY约束PK_T_CRProviders。无法在对象'dbo.T_CRProviders'中插入重复键。

该语句已被终止。

我的问题:



如何更新表格 T_CRSupportedResources

如何指示EF不更新表格 T_CRProviders

Btw,在SQL Server中,我看到该表 T_CRSupportedResources 有一个名为的外键FK_RW_TCRSupportedCloudResources_RW_TCRCloudProviders_Provider_Enum 其更新规则的值为 No Action

解决方案



在以下链接中查看我的问题的答案:



http:/ /social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/62f3e5bc-c972-4622-b830-e7d7fe710101


I have some urgent issue which I could not find answer for across the web.

I am using CodeFirst EF 4.3.1 and I am getting an error: Violation of PRIMARY KEY constraint 'PK_T_CRProviders'. Cannot insert duplicate key in object 'dbo.T_CRProviders'.

My code is:

Models:

public enum CRProviderEnums
{
    PE_Abcd = 0,
    PE_Efgh
}

[Table("T_CRProviders")]
public class CRProvider
{
    [Key]
    [Required]
    public int Enum { get; set; }
    [Required]
    public string Name { get; set; }
}

[Table("T_CRSupportedResources")]
public class CRSupportedResource
{
    [Key]
    public Guid SupportedResourceId { get; set; }
    [Required]
    public CRProvider Provider { get; set; }
}

DbContext:

public class RSContext : DbContext
{
    public DbSet<CRProvider> CRProviders { get; set; }
    public DbSet<CRSupportedResource> CRSupportedResources { get; set; }
}

Table T_CRProviders looks like this: Enum (PK), Name

Table T_CRSupportedResources looks like this: SupportedResourceId (PK), Provider_Enum (FK).

In the database table T_CRProviders I already have a provider with the following values:

Enum: 0 (which is PE_Abcd)
Name: "PE_Abcd"

Now my main() calls a method AddSupportedResource. This method adds to table T_CRSupportedResources a new CRSupportedResource which refers to provider 0 (PE_Abcd). The method looks like this:

public void AddSupportedResource()
    {
        CRSupportedResource supportedResource = new CRSupportedResource()
        {
            SupportedResourceId = Guid.NewGuid(),
            Provider = new CRProvider()
            {
                Enum = (int)CRProviderEnums.PE_Abcd,
                Name = "PE_Abcd"
            }
        };

        using (RSContext myContext = new RSContext())
        {
            myContext.CRSupportedResources.Add(supportedResource);

            myContext.SaveChanges();
        }
    }

I expect that this method will leave table T_CRProviders untouched, and add a new row to table T_CRSupportedResources which will look like this:

SupportedResourceId: DE532083-68CF-484A-8D2B-606BC238AB61
Provider_Enum (FK): 0 (which is PE_Abcd).

Instead, upon SaveChanges, Entity framework also tries to add Provider to the T_CRProviders table, and since such a provider already exists it throws the following exception:

An error occurred while updating the entries.

Violation of PRIMARY KEY constraint 'PK_T_CRProviders'. Cannot insert duplicate key in object 'dbo.T_CRProviders'.

The statement has been terminated.

My question:

How can I instruct the EF not to update table T_CRProviders upon updating table T_CRSupportedResources?

Btw, in the SQL Server I see that table T_CRSupportedResources has a foreign key named FK_RW_TCRSupportedCloudResources_RW_TCRCloudProviders_Provider_Enum and its Update Rule has the value of No Action.

解决方案

Actually, there is a way to do this.

See the answer to my question in the following link:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/62f3e5bc-c972-4622-b830-e7d7fe710101

这篇关于实体框架代码首先 - 不能在对象'dbo.T_CRProviders'中插入重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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