This question already has an answer here:
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.
Base Abstract Class
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; }
}
}
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; }
}
}
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();
}
}
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.
- This doesn't seem like the best way to approach the issue. Can I add this code somewhere else and get the actual results I want without screwing up the Users and Roles models?
- I would like to be able to do something like this with the fluent 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.
All configurations for your identity entites are defined into
IdentityDbContext<ApplicationUser>
. So if you create your custom contextApplicationDbContext
which derives fromIdentityDbContext<ApplicationUser>
you need to call this linebase.OnModelCreating(modelBuilder);
before adding configurations for own entities like below: