locked
Table-per-Concrete Type Inheritance and Primary Keys Collisions RRS feed

  • Question

  • I have 3 classes : Base, DerivedA and DerivedB that maps to two tables : A and B.

    A and B both have primary keys defined as identity. Which means both tables have their own set of primarykeys ranging from 1 to 2^64 (bigint).

    The problem i'm experiencing is Entity Framework tells me all objects in the entity set BaseSet must have unique primary keys. However, an instance of type DerivedA and an instance of type DerivedB both have the same primary key value.

    How can i resolve this problem ?
    Wednesday, November 26, 2008 2:11 PM

Answers

All replies

  • In fact, I don't think that you can. You have the same problem with horizontal entity splitting.

    I suggest you to use a uniqueidentifier PK if you can change your DB.

     

    Wednesday, November 26, 2008 3:16 PM
  • What might help is taking the identity values from disjoint sets. For instance, keys for A are always odd integers and keys for B are always even integers.

    Your table definitions might look something like this:

    CREATE TABLE A
    (
    [Id] [bigint] IDENTITY (1, 2) NOT NULL
    )
    // here, the starting point for identity is 1, and increment is 2, so odd keys will be generated

    CREATE TABLE B
    (
    [Id] [bigint] IDENTITY (2, 2) NOT NULL
    )
    // here, the starting point for identity is 2, and increment is 2, so even keys will be generated
    Wednesday, November 26, 2008 7:13 PM
  • Given that the designer does not support TPC inheritance, I am assuming you are writing mappings by hand. In that case, you should be able to place DerivedA and DerivedB in separate EntitySets, in which case the system will not complain.
    Wednesday, November 26, 2008 11:03 PM
  • I need them on the same entity set.

    I think i'm going to use uniqueidentifiers. .NET type is Guid, right ?
    Wednesday, November 26, 2008 11:07 PM
  • Yes.

    Thursday, November 27, 2008 1:24 AM