简体   繁体   English

派生自动生成的实体类

[英]Derive Auto-Generated Entity Class

I have a edmx file being set its Code Generation Strategy to None and a T4 template set to it, where I removed the Factory Method Creation logic. 我有一个edmx文件,将其Code Generation Strategy设置为None并为其设置了T4模板,在其中删除了Factory Method Creation逻辑。 I have introduced some additional method for few of the entities on a separate file through partial class. 通过局部类,我为单独文件中的少数实体引入了一些其他方法。

Ex: I have introduced few methods for the entity User on partial class and I derived the classes Admin and Person from User where I want to introduce some other methods. 例如:我为部分类上的实体User引入了几种方法,我从User派生了AdminPerson类,在此我想引入一些其他方法。

The issue I'm facing here is, while assigning a value to the navigation property of Admin , it throws the exception Object mapping could not be found for Type with identity 'CivilRegistry.ControlledModel.Admin'. 我在这里面临的问题是,在为Admin的导航属性分配值时,它引发了异常, Object mapping could not be found for Type with identity 'CivilRegistry.ControlledModel.Admin'.

User Class: 用户类别:

public partial class User
{

    protected static UserRepository repository = new UserRepository();

    public User Insert(User user)
    {
        user.AddedDate = DateTime.Now;
        user.AddedUserId = this.UserId;
        return repository.Insert(user);
    }
    //
    //Other methods goes here.
    //
}

Admin Class: 管理员班:

public class Admin : User
{
    public Admin() { }

    private Admin(User user)
    {
        this.UserName = user.UserName;
        //
        //Other properties
        //
        this.AddedUser = user.AddedUser; //This line throws, Exception.
    }

    public static Admin FindBy(int id)
    {
        //repository.FindByID returns an instance of User entity.
        return new Admin(repository.FindByID(user => user.UserId == id && user.RoleId == (int)RoleEnum.Admin));
    }
}

How can I resolve this? 我该如何解决?

It is not supported. 不支持。 You cannot define entity in EDMX and then derive additional classes from the entity in your code. 您无法在EDMX中定义实体,然后在代码中从该实体派生其他类。 Derived classes created this way are not entities any more and cannot be retrieved or persisted by EF because EF doesn't know how to map them anymore. 通过这种方式创建的派生类不再是实体,并且由于EF不再知道如何映射它们,因此EF无法对其进行检索或保留。

The reason why it fails in assigning navigation property is that EF knows that Admin is User and it tries to attach it to the context but it doesn't find the Admin type in mapping (EDMX). 它未能分配导航属性的原因是,EF知道AdminUser并且它尝试将其附加到上下文,但未在映射(EDMX)中找到Admin类型。

The solution is mapping the inheritance = moving your inheritance to EDMX where you will define User entity and derived Person and Admin entities. 解决方案是映射继承=将继承移动到EDMX,您将在其中定义User实体以及派生的PersonAdmin实体。 Here you have some tutorial. 在这里,您有一些教程。

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

相关问题 自动生成的实体类中缺少导航属性 - missing Navigation property in auto-generated entity class 指定Web服务引用自动生成的实体基类 - Specify Web Service Reference Auto-Generated Entity base class 如何在Entity Framework中为自动生成的类添加自定义方法? - How to add custom method to auto-generated class in Entity Framework? 循环进入类元数据中的自定义属性(以避免通过实体框架工作图修改自动生成的类) - Looping into custom attributes in class Metadata (to avoid modifying auto-generated class by Entity FrameWork diagram) 自定义自动生成的类的显示 - Customise the display of a auto-generated class 将方法添加到自动生成的类 - Add method to auto-generated class 实体框架中自动生成的实体的基类类型是什么? - What is the base class type of the auto-generated entities from the Entity Framework? C#按实体框架向自动生成的表中添加一列 - C# Add a column to Auto-Generated Table by Entity Framework 如何摆脱自动生成模型中的Entity Framework属性? - How to get rid of Entity Framework attributes in auto-generated models? 继承自Entity Framework中的自动生成的类 - Inheriting from auto-generated classes in Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM