[英]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
派生了Admin
和Person
类,在此我想引入一些其他方法。
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知道
Admin
是User
并且它尝试将其附加到上下文,但未在映射(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
实体以及派生的Person
和Admin
实体。 Here you have some tutorial. 在这里,您有一些教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.