TPT继承的实体框架代码优先重写onModelCreating()破坏身份用户和角色模型 [英] Entity Framework Code First override onModelCreating() for TPT Inheritance Screwing Up Identity User and Role Models

查看:78
本文介绍了TPT继承的实体框架代码优先重写onModelCreating()破坏身份用户和角色模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Fluent API扩展到继承类时遇到问题.我采用了TPT(每种类型的表)方法,每种继承类型都有一个表.我喜欢每种类型的表,因为数据库已完全规范化且易于维护.我在覆盖onModelCreating方法时遇到错误,该方法

I'm having problems extending the Fluent API to my inheritance classes. I have taken the TPT (table per type) method and each type of the inheritance has a table. I like table per type because the database is fully normalized and easy to maintain. I am getting an error with overiding the onModelCreating method that this article instructed me to do. It is losing the keys to the User and Roles Entities.

基本抽象类

namespace Home_Warranty.Models
{
    public abstract class Vendor
    {
        [Key]
        public int VendorID { get; set; }

        [Required]
        public string CompanyName { get; set; }

        [Required]
        public int StreetNumber { get; set; }

        [Required]
        public string StreetName { get; set; }

        }
}

从供应商继承的ServiceCompany类

Inherited ServiceCompany Class from Vendor

namespace Home_Warranty.Models
 {
 [Table("ServiceCompanies")]
 public class ServiceCompany : Vendor
 {
    public string ACHClaim { get; set; }

    public virtual ICollection<SubContractorCompany> SubContractorCompanies{ get; set; }

    public virtual ICollection<ServiceCompanyUser> SubContractorUsers { get;set; }
    }
}

我在其中添加了实体模型以通过onModelCreating()

Where I added the entity models to enable the Fluent API with onModelCreating()

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public DbSet<Vendor> Vendors { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ServiceCompany>().ToTable("ServiceCompanies");

    }


    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

在更新数据库时,我在迁移时遇到以下错误.绝对是因为我覆盖了onCreateModel函数.

I'm getting the following error on my migration while updating database. It definitely because of me overiding onCreateModel function.

at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
One or more validation errors were detected during model generation:
Home_Warranty.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
Home_Warranty.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

  1. 这似乎不是解决问题的最佳方法.我可以在不增加用户和角色模型的情况下,将此代码添加到其他地方并获得所需的实际结果吗?
  2. 我希望能够使用流畅的API进行类似的操作.

var ListofServiceCompanies = db.ServiceCompanies.All()

还是我应该只是通过供应商实体?

or should I just go through the Vendor Entity instead?

var ListofServiceCompanies = db.Vendor.SelectMany( Vendor is a ServiceComapny...etc)

我更喜欢正确地设置实体,并使代码易于使用.任何见识或知识都会受到赞赏.

I prefer to set up the entities correctly and make the code nice and easy to use. Any insight or knowledge is appreciated.

推荐答案

身份实体的所有配置都定义在IdentityDbContext<ApplicationUser>中.因此,如果您创建自IdentityDbContext<ApplicationUser>派生的自定义上下文ApplicationDbContext,则需要在添加如下所示的自己实体的配置之前调用此行base.OnModelCreating(modelBuilder);:

All configurations for your identity entites are defined into IdentityDbContext<ApplicationUser>. So if you create your custom context ApplicationDbContext which derives from IdentityDbContext<ApplicationUser> you need to call this line base.OnModelCreating(modelBuilder); before adding configurations for own entities like below:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<ServiceCompany>().ToTable("ServiceCompanies");
}

这篇关于TPT继承的实体框架代码优先重写onModelCreating()破坏身份用户和角色模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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