1

I use Oracle EF 5.0 provider. Oracle 11G database. Here is my database schema: Database scheme

Table has an Id as primary key. For each table in database there are trigger, that fires on insert new record and EF get primary key after insert from sequence. Sequences are created for each table. In my edmx file each ID column has an StoreGeneratedPattern="Identity" attribute set.

My code is:

 using (var context = new Entities())
 {
    var title = new TITLE
    {
        TITLE_NUMBER = 4000001,
        IS_DRAFT = "N", 
        REGISTRY_DATE = DateTime.Now
    };

    var titleName = new TITLE_NAME
    {
        NAME = "title name"
    };
    title.TITLE_NAME.Add(titleName);
    context.Set<TITLE>().Add(title);
    context.SaveChanges();
 }

When context.SaveChanges() are executed, there is exception thrown:

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

But changes are saved in database correctly. And my Title object has correct Id for Title and for Title_Name. What can I do? Or what I do wrong?

Opps. I remove inheritance from edmx and error goes away!....

But all project is build in inheritance!!!

Ops. MS SQL with same scheme gives this exception too.

1 Answer 1

0

From here: https://entityframework.codeplex.com/workitem/2515
EF Team Triage: This is because you have identity configured with a TPC mapping. Because the two types are mapped to completely separate tables, you will get duplicate IDs generated since the database doesn't know to generate unique values between the two tables. One way around this is to modify the tables to have one generate odd numbers and the other generate even numbers. Workaround for this.

We create Interface with shared properties for all entities and remove inheritance from edmx file.
I.e.

public interface IHistoricalEntity
{
    int ID { get; set; }
    int ENTITY_ID { get; set; }
    DateTime CREATE_DATE { get; set; }
    DateTime DELETE_DATE { get; set; }
    int CREATOR { get; set; }
}

in partial file TITLE.cs

public partial class TITLE : IHistoricalEntity
{
}

and thus we can use generic version for all entities in our project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.