The documentation states:
Inheritance hierarchies that include owned entity types are not supported
The practical upshot of this is that any class that inherits from IdentityUser cannot reliably include an owned type. This is turning out to be a major headache.
So, I have two questions:
@sbsw This means that owned entity types cannot themselves be in an inheritance hierarchy. It does not mean that entity types that are part of an inheritance hierarchy cannot have owned types. For example, this is fine:
```C#
public class Blog
{
public int Id { get; set; }
}
public class GoodBlog : Blog
{
public Address HomeAddress { get; set; }
public Address WorkAddress { get; set; }
}
public class Address
{
public string Street { get; set; }
public string Zip { get; set; }
}
```C#
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>();
modelBuilder.Entity<GoodBlog>(b =>
{
b.OwnsOne(e => e.HomeAddress);
b.OwnsOne(e => e.WorkAddress);
});
}
For the not supported case, see #9630
Oh. In that case, I seem to have stumbled across a bug. I’ll see if I can re-create the issue in a simple example, and open a new issue on that.
I will also put in a documentation issue to clarify the inheritance limitation.
Bob
Sent from my iPhone. Bizarre, humorous, or nonsensical autocorrects are a "feature".
On Jan 17, 2019, at 2:52 PM, Arthur Vickers notifications@github.com wrote:
@sbsw This means that owned entity types cannot themselves be in an inheritance hierarchy. It does not mean that entity types that are part of an inheritance hierarchy cannot have owned types. For example, this is fine:
public class Blog
{
public int Id { get; set; }
}public class GoodBlog : Blog
{
public Address HomeAddress { get; set; }
public Address WorkAddress { get; set; }
}public class Address
{
public string Street { get; set; }
public string Zip { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity();
modelBuilder.Entity(b =>
{
b.OwnsOne(e => e.HomeAddress);
b.OwnsOne(e => e.WorkAddress);
});
}
For the not supported case, see #9630—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Sorry if I'm commenting on this closed issue in error, but I seem to be having the following related issue to inheritance and owned entity types.
Given the following:
[Owned]
public class FileGuid
{
public Guid Guid { get; set; }
public string FileExtension { get; set; }
}
public abstract class Asset
{
public Guid Guid { get; set; }
public string Name { get; set; }
}
public class MaterialAsset : Asset
{
public int Color { get; set; }
public bool Transparent { get; set; }
public float Opacity { get; set; }
public FileGuid TextureAsset { get; set; }
public FileGuid DecalTextureAsset { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Asset>().HasKey(a => a.Guid);
modelBuilder.Entity<MaterialAsset>().HasBaseType<Asset>();
modelBuilder.Entity<MaterialAsset>().OwnsOne(ma => ma.TextureAsset, o =>
{
o.Property(fg => fg.Guid).HasColumnName("TextureAsset_Guid");
o.Property(fg => fg.FileExtension).HasColumnName("TextureAsset_FileExtension");
});
modelBuilder.Entity<MaterialAsset>().OwnsOne(ma => ma.DecalTextureAsset, o =>
{
o.Property(fg => fg.Guid).HasColumnName("DecalTextureAsset_Guid");
o.Property(fg => fg.FileExtension).HasColumnName("DecalTextureAsset_FileExtension");
});
}
Gives the following exception:
System.InvalidOperationException: The keys {'Guid'} on 'MaterialAsset.TextureAsset#FileGuid' and {'Guid'} on 'Asset' are both mapped to 'Assets.PK_Assets' but with different columns ({'TextureAsset_Guid'} and {'Guid'}).
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateSharedKeysCompatibility(IReadOnlyList`1 mappedTypes, String tableName)
at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.ValidateSharedKeysCompatibility(IReadOnlyList`1 mappedTypes, String tableName)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateSharedTableCompatibility(IModel model)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model)
at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel model)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ValidatingConvention.Apply(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate()
at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__1()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_2(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
When I don't use the OwnsOne() method to define the relationship, it will not give an exception when creating migration, but when inserting records into the database, as follows:
System.InvalidOperationException: The foreign key '{Guid: 1a2bf94e-e2a0-4ced-a4d6-121f5a5c20fd}' set on 'MaterialAsset.TextureAsset#FileGuid' with the key value '{Guid: 1a2bf94e-e2a0-4ced-a4d6-121f5a5c20fd}' matches an entity of type 'TextureAsset', however the principal entity type should be assignable to 'MaterialAsset'.
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet`1 handledForeignKeys, Boolean fromQuery)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Nullable`1 forceStateWhenUnknownKey)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node, Boolean force)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode node, TState state, Func`3 handleNode)
at at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey)
at at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
at at Microsoft.EntityFrameworkCore.DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState)
at at Microsoft.EntityFrameworkCore.DbContext.AddRange(IEnumerable`1 entities)
at at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.AddRange(IEnumerable`1 entities)
... local initialize code ...
This does NOT happen when I rename the Guid property of the Asset class to GuidTest, so there is no overlap on inherited properties. And the following columns are created as expected (with or without the OwnsOne() method):
TextureAsset_GuidTextureAsset_FileExtensionDecalTextureAsset_GuidDecalTextureAsset_FileExtension.Is this expected behavior or a known limitation?
And if so is the only work around simply using different names for the properties?