Entity Framework Code First - Cannot insert duplicate key into object 'dbo.T_CRProviders'

I have an urgent problem that I could not find an answer for the entire network.

I am using CodeFirst EF 4.3.1 and I get the error message: Violation of PRIMARY KEY constraint 'PK_T_CRProviders'. Cannot insert duplicate key in object 'dbo.T_CRProviders'. 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; } } 

The T_CRProviders table is as follows: Enum (PK), Name

The T_CRSupportedResources table is as follows: SupportedResourceId (PK), Provider_Enum (FK).

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

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

Now my main () calls the AddSupportedResource method. This method adds a new CRSupportedResource to the T_CRSupportedResources table, which refers to provider 0 (PE_Abcd). The method is as follows:

 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 this method to leave the T_CRProviders table intact and add a new row to the T_CRSupportedResources table, which will look like this:

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

Instead, in SaveChanges, the Entity framework is also trying to add a 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 is:

How can I tell EF not to update the T_CRProviders table after updating the T_CRSupportedResources table?

Btw, in SQL Server, I see that the T_CRSupportedResources table has a foreign key named FK_RW_TCRSupportedCloudResources_RW_TCRCloudProviders_Provider_Enum , and its update rule is set to No Action .

+4
source share
2 answers

Actually, there is a way to do this.

See the answer to my question at the following link:

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

0
source

I expect this method to leave the T_CRProviders table intact, and add a new row to the T_CRSupportedResources table

No, that will not happen. You create a separate object graph consisting of the existing object a and the new object. EF does not know about the existence of your entity until you report it - there are no database queries confirming the existence that EF is executing behind.

If you call the Add method, all entities in the entity graph are added as new. If you do not want to embed them all, you can start by using Attach and manually change the state for new ones. For example, for example:

 myContext.CRSupportedResources.Attach(supportedResource); myContext.Entry(supportedResource).State = EntityState.Added; 
+6
source

Source: https://habr.com/ru/post/1400957/


All Articles