-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The entity type is part of a hierarchy, but with no discriminator property configured #51
Comments
I found a solution. Apparently, this provider doesn't automatically do registrations for derived classes, so you need to do them manually by overriding the Is there a particular reason as to why I need to do this? Shouldn't the provider do this automatically? public class DokflytContext: DbContext
{
public DokflytContext(DbContextOptions<DokflytContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Equipment> Equipments { get; set; }
public DbSet<Loan> Loans { get; set; }
public DbSet<Maintenance> Maintenances { get; set; }
public DbSet<Project> Projects { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Manually configure discriminators since the
// MySQL provider doesn't do it implicitly.
builder
.Entity<Loan>()
.HasDiscriminator<string>("Discriminator")
.HasValue<Loan>(nameof(Loan))
.HasValue<Maintenance>(nameof(Maintenance));
builder
.Entity<Loaner>()
.HasDiscriminator<string>("Discriminator")
.HasValue<Loaner>(nameof(Loaner))
.HasValue<User>(nameof(User))
.HasValue<Project>(nameof(Project));
}
} |
I'm not sure. Do you know of other providers that don't behave this way? |
The SQLite provider didn't behave this way. I haven't tested the other ones. |
I took a quick search through the SQLite provider code, as well as through npgsql, and didn't see anything obvious around discriminators. What is the name of the column that SQLite creates as your discriminator column? |
The name of the column is simply This is the generated migration code for the migrationBuilder.CreateTable(
name: "Loaner",
columns: table => new
{
LoanerId = table.Column<int>(nullable: false)
.Annotation("MySQL:AutoIncrement", true),
Discriminator = table.Column<string>(nullable: false),
PName = table.Column<string>(nullable: true),
Email = table.Column<string>(nullable: true),
FirstName = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
Password = table.Column<string>(nullable: true),
Role = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Loaner", x => x.LoanerId);
});
|
Ok, that helped a lot in finding where this happens. I believe this behavior to be part of the RelationalConventionSetBuilder, which SqlServer, Sqlite, and Npgsql are all using, but we are not. It seems like something we should use, but I'm a little worried about it causing uenxpected behavior with current systems. I'll try wiring it up, and will bump this library by a minor version, rather than a revision. |
@ts95 Can you give this version a try and see if it behaves as you expected? https://ci.appveyor.com/api/buildjobs/nycxtw9od923rciu/artifacts/artifacts%2FSapientGuardian.EntityFrameworkCore.MySql%2FSapientGuardian.EntityFrameworkCore.MySql.7.2.0.nupkg |
Restore output
I still get the same error as before.
|
You could reproduce this by creating a model that inherits from another one. Base class class Animal
{
[Key]
public int AnimalId { get; set; }
public int Weight { get; set; }
} Subclass Dog class Dog : Animal
{
public string Breed { get; set; }
} Subclass Cat class Cat : Animal
{
public bool IsHairless { get; set; }
} If you create a migration for this it should generate just one table (called Instead of generating it it simply throws the exception I posted above aka |
@ts95 try abstract class Animal, like: |
I tried to migrate from using SQLite to MySQL for my project, and I keep getting this error:
System.InvalidOperationException: The entity type 'Loan' is part of a hierarchy, but does not have a discriminator property configured.
My Loan model looks like this:
Adding a
Discriminator
property did not change anything.I have a different model that inherits from the
Loan
model:What could be causing this problem and how can I solve it?
The text was updated successfully, but these errors were encountered: