实体框架TPH继承数据建模问题 [英] Entity Framework TPH Inheritance Data Modeling Issues

查看:148
本文介绍了实体框架TPH继承数据建模问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Entity Framework和C#/ .Net的新手,并尝试创建一个TPH继承模型,我不知道我是否应该,否则,请指教,

I'm new to Entity Framework and C#/.Net and trying to create a TPH inheritance model, I'm not sure if I should be or not, so if not, please advise,

这是模型:

 public abstract class Vote
 {
    public int VoteID { get; set; }
    public int UserID { get; set; }
    public bool Value { get; set; }
    public DateTime DateCreated { get; set; }

    public User User { get; set; }
 }

 public class ProjectVote_ : Vote
 {
    public int ProjectID { get; set; }
    public virtual Project Project { get; set; }
 }
 public class CommentVote_ : Vote //There are three more like this, votes for different hings
 {
    public int CommentID { get; set; }
    public virtual Comment Comment { get; set; }
 }

现在项目模型(注释和模型是类似的)

Now the Project model (comment and model is similar)

public class Project
{
    public int ProjectID { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Vote> Vote { get; set; }
}

发生的是ICollection创建一个数据库列Project_ProjectID作为外键投票表(我认为)而不是使用我定义的ProjectID。我如何解决它,或者我应该模型不同。如果流利的API是修复它的方法,我不知道该怎么做。

What happens is that ICollection creates a database column Project_ProjectID as the foreign key in the Vote table (I think) instead of using the ProjectID I defined. How do I fix it or should I model it differently. If the fluent API is the way to fix it, I don't know how to do that.

最后我想要使用一个表来存储5种不同类型的投票。

In the end I want to be able to use one table to store 5 different types of votes.

推荐答案

当您有相关实体时,您不需要有一个属性将FK存储在模型中。实体框架知道,当您在ProjectVote_模型中检测到Project时,需要在ProjectVote中的Project表中创建FK。与User和UserId以及Comment和CommentId相同。您不需要在模型中存储FK的属性。

When you have related entities you don't need to have a property to store the FK in your model. Entity framework knows that it needs to make a FK to the Project table in ProjectVote when it detects Project in your ProjectVote_ model. Same thing with User and UserId and Comment and CommentId. You don't need to have a property that stores the FK in your model.

您正在获取名为Project_ProjectID的FK列,因为Entity框架正在检测到需要为您的导航属性创建一个FK项目。它正在使用它自己的命名约定来创建列,因此Project_ProjectID。

You are getting the FK column with the name you don't like "Project_ProjectID" because Entity framework is detecting that it needs to create a FK for your navigation property "Project". It's using it's own naming convention to create the column hence "Project_ProjectID".

如果要为DBContext类中的列重写OnModelCreating提供自己的名称,并将其添加流畅的映射。

If you want to provide your own name for the column override OnModelCreating in your DBContext class and add this fluent mapping.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Project>()
        .HasMany(p => p.Vote)
        .HasRequired(v => v.Project) //or .WithOptional(v => v.Project)
        .Map(m => m.MapKey("ProjectId"));  //or any other name you want.
}

未来这是一个有用的参考,如何使用Fluent API。例如这里有一些文档关于如何通过流利来统一TPH。

And for the future this is a helpful reference for how to use the Fluent API. For example here is some documentation on how to custimize TPH with fluent.

希望帮助!

这篇关于实体框架TPH继承数据建模问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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