EntityTypeConfiguration在Entity Framework中跨不同项目的继承 [英] Inheritance of EntityTypeConfiguration across different Projects in Entity Framework

查看:100
本文介绍了EntityTypeConfiguration在Entity Framework中跨不同项目的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主 Visual Studio项目,其中包含类似于以下内容的实体框架映射:

I have a 'master' Visual Studio project which contains Entity Framework mappings similar to:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        // Table Mapping
        ToTable("Users");
        this.Property(x => x.Username).HasColumnName("Username");


        // Other mapping goes here
    }
}

正在映射的 User 实体只是一个简单的POCO:

The User entity being mapped is just a simple POCO:

 public class User {
     public string Username { get; set; }

     // Other properties omitted for brevity
 } 

我有第二个子 VS项目/应用程序(引用主项目),它具有以下 ExtendedUser 实体/ POCO,它也使用EF映射:

I have a second 'child' VS project/application (which references the master project) which has the following ExtendedUser Entity/POCO which is also mapped using EF:

public class ExtendedUser : User {

     // Extra navigation properties
     public ICollection<Order> Orders { get; set; }
}

此实体没有多余的字段,但是有各种关系集合

This entity doesn't have extra fields, but does have various relationship collections which are specific to that application and only that application.

我的问题是我想继承为 User User 类将在其他几个项目中使用,因此我需要避免任何重复的映射。

My problem is that I would like to inherit the mapping defined for the User in the first VS project. The master User class is going to be used in several other projects so I need to avoid any duplication of mappings.

如果我将映射定义为:

public class ExtendedUserMap : UserMap
{
    public UserMap()
    {

    }
}

然后我无法引用任何 ExtendedUser 属性,因为映射的类型为 EntityTypeConfiguration< User> 而不是 EntityTypeConfiguration< ExtendedUser>

Then I can't reference any of the ExtendedUser properties as the mapping is of type EntityTypeConfiguration<User> not EntityTypeConfiguration<ExtendedUser>.

很显然,我不能从两个类继承,所以我不确定实现我想做的事情的合适方法。

Obviously I can't inherit from two classes, so I am unsure of a suitable way to achieve what I want to do.

如何定义 ExtendedUserMap ,以便可以使用 User 映射并包含导航 ExtendedUser 的属性?

How can I define ExtendedUserMap such that I can use the User mappings and also include the navigation properties for ExtendedUser?

推荐答案

您可以稍微定义基本映射类不同:

You can define your base mapping class slightly different:

public abstract class UserMapBase<TUser> : EntityTypeConfiguration<TUser>
    where TUser : User
{
    protected UserMapBase()
    {
        // Table Mapping
        ToTable("Users");
        this.Property(x => x.Username).HasColumnName("Username");


        // Other mapping goes here
    }
}

现在您可以拥有这样的子类:

Now you can have a subclasses like so:

public class UserMap : UserMapBase<User>
{ }

public class ExtendedUserMap : UserMapBase<ExtendedUser>
{
    public ExtendedUserMap()
    {
        // map ExtendedUser properties here.
    }
}

这篇关于EntityTypeConfiguration在Entity Framework中跨不同项目的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆