entity-framework - EF Core Hierarchy TPH map two entities to one
问题描述
I have a base project, which has some basic entities. Let's use BaseUser
as one. In the child project, it references base project and User
class inherits from that BaseUser
. The only difference is the User
class has List<Blogs>
, so no additional properties, no change in mapping.
When I query Users
, it does not find any because they are created as BaseUser
and the query has discriminator value of "User". I don't want to query "BaseUser" because I want the relational property of List<Blogs>
.
Is there any way to tell EF to basically treat these classes as one? Is there a better way to handle the split? (obviously base project has no concept of blogs, so cannot move the List to the base)
Some sample classes as requested
/* Base Project (nuget package created) */
public class BaseUser {
public int UserId { get; set; }
public int Name { get; set; }
}
public class BaseContext : DbContext {
public DbSet<BaseUser> BaseUsers {get;set;}
}
public class BaseDataInstaller {
BaseContext _ctx;
public BaseDataInstaller( BaseContext ctx ){
_ctx = ctx;
}
public void Install(){
_ctx.BaseUsers.Add( new BaseUser { Name="Demo User 1" } );
_ctx.BaseUsers.Add( new BaseUser { Name="Demo User 2" } );
_ctx.SaveChanges();
}
}
/* Child Project (consumes nuget package)*/
public class User : BaseUser {
List<Blogs> Blogs { get; set; }
}
public class ProjectContext : BaseContext {
public DbSet<User> Users { get; set; }
}
public class SomeService {
ProjectContext _ctx;
public BaseDataInstaller(ProjectContext ctx){
_ctx = ctx;
}
//Finds 0 users
public void PrintUsers(){
var users = _ctx.Users.ToList();
users.ForEach( u=> Console.WriteLine(u.Name) );
}
//Finds Users
public void PrintBaseUsers(){
var users = _ctx.BaseUsers.ToList();
users.ForEach( u=> Console.WriteLine(u.Name) );
}
}
Comparing the SQL generated, there is a discriminator added
where Discriminator = 'BaseUser'
or where Discriminator = 'User'
There are no properties which are different between the two, just the relationship with the blogs.
So is there a way to either make both have the same Discriminator value or another way to solve this?
UPDATE 1
The discriminator only appears if the DbContext knows about BOTH entities. if it only knows about the one, it is happy to map onto the table. Even if the child inherits from the base, it still doesn't need a discriminator. So I think the challenge is to re-work the base so the context doesn't know about the base. This does feel like a workaround though. Maybe the structure should change:
instead of User : BaseUser
use a property
User
- int ChildUserId {get; set;}
- BaseUser BaseUser {get; set;}
- SomeObject SomeNavProperty etc
it will mean a new table for each inherited project, but would allow the project to add it's own specific data too...
解决方案
推荐阅读
- node.js - 生成客户端令牌时出错(PayPal 和 Firebase 函数)
- javascript - 循环中的重复数据
- sql-server - 如何遍历从 PowerShell 运行的 SQL 的结果(system.Data.DataSet 对象)
- javascript - Webpack 4 css模块TypeError:无法读取未定义的属性“上下文”
- android - Android - Kotlin - 如何迭代组子元素
- javascript - 隐藏垂直滚动条
- python - 零钱计算器仅在需要一种硬币时才有效
- python - 在初始化时从其他参数创建 luigi 参数
- c# - 将 HTML 属性添加到匿名类型而不更改方法签名
- asp.net - TeamCity 使用 MSDEPLOY ERROR_FILE_IN_USE