efcore icon indicating copy to clipboard operation
efcore copied to clipboard

error mapping sql types in derived types

Open esargent opened this issue 10 years ago • 4 comments

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);
    }
}

}

esargent avatar Jan 07 '16 22:01 esargent

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)

smitpatel avatar Jan 08 '16 00:01 smitpatel

Seems like we should throw if the types don't match. Should this happen in model validation of in migrations?

rowanmiller avatar Jan 08 '16 00:01 rowanmiller

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?

smitpatel avatar Jan 08 '16 00:01 smitpatel

It would make sense to validate this in model validation. See https://github.com/aspnet/EntityFramework/issues/4074

AndriySvyryd avatar Jan 08 '16 01:01 AndriySvyryd