简体   繁体   English

实体框架TPC复制的主键

[英]Entity Framework TPC duplicated primary keys

I am aware that when using table per concrete type inheritance all derived classes share the same identity. 我知道当使用每个具体类型继承的表时,所有派生类共享相同的标识。 What I would like to do is to have a seperate identity for Guest and AccountOwner. 我想做的是为GuestAccountOwner分别拥有一个身份

After hours of googling I found two solutions. 经过数小时的搜寻,我找到了两种解决方案。 One is to use different seeds for derived classes, (I know how to implement this, but I dont like it), and the second one is to use different identity increments. 一种是对派生类使用不同的种子(我知道如何实现,但我不喜欢它),第二种是使用不同的标识增量。

Base class: 基类:

public abstract class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
}

Derived class: 派生类:

public class Guest : User
{
    //Other properties here
}

Derived class: 派生类:

public class AccountOwner: User
{
    //Other properties here
}

If I were to use different identity increments, primary keys of Guest would look something like: 1, 3, 5, 6 and AccountOwner 2, 4, 6, 8. 如果我使用不同的身份增量,则Guest的主键将类似于: 1、3、5、6AccountOwner 2、4、6、8。

The question now is, 现在的问题是

How do I implement different primary keys auto increments? 如何实现不同的主键自动递增?

Have I missed something? 我错过了什么吗? Do you have a better solution? 您有更好的解决方案吗?

I dont want to change my db structure. 我不想更改我的数据库结构。

You can't implement different auto increments. 您不能实现不同的自动增量。

I have decided to go with Table per Hierarchy instead of Table per Hierarchy even tho I didn't want to change my structure. 即使我不想更改我的结构,我也决定使用“每个层次的表”而不是“每个层次的表”。

Instead of three different tables I have now one, Accounts, and I'm using a Descriminator for the different types. 现在不再是三个不同的表,而是一个“帐户”,而我对不同类型使用了一个描述符。 Because I only have one table they all share the same auto increment. 因为我只有一个表,所以它们都共享相同的自动增量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM