error mapping sql types in derived types
This is an odd one I found while trying to repo a different issue. Notice that SpecialUser1 and 2 both derive from ApplicationUser. Also notice that they both define "Sex" but of different type. Probably this is an error on the dev's part, but in more complex hierarchies maybe that happens.
The problem is that EF scripts successfully into the database with "sex" column of type int, while SpecialUser2.Sex is type string! I'd expect an error to be thrown.
This is with RC2 16649. Did you loosen up error checking since RC1 - because that might explain why I can't repo the other issue I had where defining the same property in two sibling derived types threw an error, even if they used the same type. (https://github.com/aspnet/EntityFramework/issues/4224)
using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity;
namespace WebApplication5 { public class ApplicationUser : IdentityUser { } public class SpecialUser1 : ApplicationUser { //same property in SpecialUser1 and SpecialUser2 public char Sex { get; set; } }
public class SpecialUser2 : ApplicationUser
{
public string SomethingElse { get; set; }
public string Sex { get; set; } //same property NAME in SpecialUser1 and SpecialUser2
public string SomethingElse2 { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
//two derived classes with same property NAME as each other, but different types
public DbSet<SpecialUser1> Users1 { get; set; }
public DbSet<SpecialUser2> Users2 { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
In file MigrationsModelDiffer.cs
private IEnumerable<IProperty> GetPropertiesInHierarchy(IEntityType entityType)
=> entityType.GetDeclaredProperties().Concat(entityType.GetDerivedTypes().SelectMany(t => t.GetDeclaredProperties()))
.Distinct((x, y) => Annotations.For(x).ColumnName == Annotations.For(y).ColumnName);
is silently merging properties from derived types which have same name (regardless of other details of property)
Seems like we should throw if the types don't match. Should this happen in model validation of in migrations?
The runtime model is presently shown as valid since both the properties are in their respective entities hence possesses all the details about the property. It can be part of model validation. @AndriySvyryd - views?
It would make sense to validate this in model validation. See https://github.com/aspnet/EntityFramework/issues/4074