简体   繁体   English

实体使用两个不同的键拆分成三个表?

[英]Entity splitting across three tables with two different keys?

I have the following database schema which I can't change: 我有以下无法更改的数据库架构:

数据库架构

The relationship between the TM_USER and TM_USER_DETAIL tables uses the USR_ID and UDT_USR_ID columns. TM_USERTM_USER_DETAIL表之间的关系使用USR_IDUDT_USR_ID列。 The data type of USR_ID is int . USR_ID的数据类型为int

The relationship between the TM_USER and TM_MEMBERSHIP_USERS tables uses the USR_USER_KEY and USR_ID columns. TM_USERTM_MEMBERSHIP_USERS表之间的关系使用USR_USER_KEYUSR_ID列。 The data type of USR_USER_KEY is a guid represented as a string . USR_USER_KEY的数据类型是用string表示的guid

My question is how to map this using Entity Framework? 我的问题是如何使用实体框架对此进行映射?

Here is my entity: 这是我的实体:

public class User
{
    // from table TM_USER
    public int Id { get; set; }
    public string Username { get; set; }
    public string Key { get; set; }

    // from table TM_USER_DETAIL
    public string Forename { get; set; }
    public string Surname { get; set; }

    // from table TM_MEMBERSHIP_USERS
    public bool IsApproved { get; set; }
}

And my entity configuration: 而我的实体配置:

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        Map(map =>
        {
            map.Properties(p => new { p.Key, p.Id, p.Username });

            map.ToTable("TM_USER");

            map.Property(p => p.Key).HasColumnName("USR_USER_KEY");
            map.Property(p => p.Id).HasColumnName("USR_ID");
            map.Property(p => p.Username).HasColumnName("USR_USER_NAME");
        });

        Map(map =>
        {
            map.Properties(p => new { p.Forename, p.Surname });

            map.ToTable("TM_USER_DETAIL");

            map.Property(p => p.Id).HasColumnName("UDT_USR_ID");
            map.Property(p => p.Forename).HasColumnName("UDT_FORENAME");
            map.Property(p => p.Surname).HasColumnName("UDT_SURNAME");
        });

        Map(map =>
        {
            map.Properties(p => new { p.IsApproved });

            map.ToTable("TM_MEMBERSHIP_USERS");

            map.Property(p => p.Key).HasColumnName("USR_ID");
            map.Property(p => p.IsApproved).HasColumnName("USR_ISAPPROVED");
        });

        HasKey(user => user.Id);
    }
}

When I try to query the Users , I get the following error: 当我尝试查询Users ,出现以下错误:

Properties for type 'User' can only be mapped once. “用户”类型的属性只能映射一次。 The non-key property 'Id' is mapped more than once. 非关键属性“ Id”被多次映射。 Ensure the Properties method specifies each non-key property only once. 确保“属性”方法仅指定每个非关键属性一次。

Is it possible to configure EF to map a single entity to three tables in this way using different keys for each relationship? 是否可以将EF配置为以这种方式为每个关系使用不同的键将单个实体映射到三个表?

I have posted a sample solution here: https://github.com/kevinkuszyk/entity-splitting 我在这里发布了一个示例解决方案: https : //github.com/kevinkuszyk/entity-splitting

This is not possible. 这是不可能的。 You should split your model into different models. 您应该将模型分为不同的模型。 Furthermore, the relationship does not make sense for me. 而且,这种关系对我来说没有意义。 In your schema, TM_USER has many TM_USER_DETAIL , that relationship allows an User to have several forenames and surnames. 在您的架构中, TM_USER具有许多TM_USER_DETAIL ,该关系允许用户具有多个TM_USER_DETAIL和姓氏。 I think that was not your intention. 我认为那不是你的意图。 In a common scenario, the TM_USER has only one TM_USER_DETAIL . 在常见情况下, TM_USER只有一个TM_USER_DETAIL So, your model should be something like this: 因此,您的模型应该是这样的:

TM_USER
-------------
UserId - PK
Username 
Key 
Deleted

TM_USER_DETAIL
---------------
UserId - PK - FK TM_USER
Forename 
Surname 

To make the model even simpler, we can merge TM_USER_DETAIL with TM_USER . 为了使模型更加简单,我们可以将TM_USER_DETAILTM_USER合并。 Like this: 像这样:

TM_USER
-------------
UserId - PK
Username 
Key 
Forename 
Surname 
Deleted

The same thing values to the relationship between TM_User and TM_MEMBERSHIP_USERS . 相同的东西对于TM_UserTM_MEMBERSHIP_USERS之间的关系TM_User TM_MEMBERSHIP_USERS So, we can merge them. 因此,我们可以将它们合并。 Like this: 像这样:

TM_USER
-------------
UserId - PK
Username 
Key 
Forename 
Surname
IsApproved  
Deleted

Now, we have to create the User class in order to use it in the Entity Framework. 现在,我们必须创建User类,以便在实体框架中使用它。

public class User
{
    // from table TM_USER 
    public int Id { get; set; }
    public string Username { get; set; }
    public string Key { get; set; }


    public string Forename { get; set; }
    public string Surname { get; set; }

    public bool IsApproved { get; set; }
}

Mapping: 对应:

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        ToTable("TM_USER");

        Property(p => p.Key).HasColumnName("USR_USER_KEY");
        Property(p => p.Id).HasColumnName("USR_ID");
        Property(p => p.Username).HasColumnName("USR_USER_NAME");
        Property(p => p.Forename).HasColumnName("UDT_FORENAME");
        Property(p => p.Surname).HasColumnName("UDT_SURNAME");
        Property(p => p.IsApproved).HasColumnName("USR_ISAPPROVED");

        HasKey(user => user.Id);
    }
}

If you still want to use 3 different tables, you have to create 3 different classes, with their respective properties, mapping them to their respective tables. 如果仍然要使用3个不同的表,则必须创建3个不同的类,并使用它们各自的属性,将它们映射到各自的表中。

Hope it helps! 希望能帮助到你!

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

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