efcore
efcore copied to clipboard
Owned shared entities doesn't appear to work with parent Entities with composite primary key.
This is a different case from issue #13986
Owned shared entities doesn't appear to work with parent Entities with composite primary key.
The code that throws the Exception (created as Console App):
using Microsoft.EntityFrameworkCore;
using System;
namespace ConsoleApp {
[Owned]
public class Site {
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
}
public class Person {
public string Id1 { get; set; }
public string Id2 { get; set; }
public Site HomeAddress { get; set; }
public Site WorkAddress{ get; set; }
}
public class TextCTX : DbContext {
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Person>(mb => {
mb.Property(x => x.Id1).IsRequired(true);
mb.Property(x => x.Id2).IsRequired(true);
mb.HasKey(x => new { x.Id1, x.Id2 });
mb.OwnsOne(x => x.HomeAddress);
mb.OwnsOne(x => x.WorkAddress);
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=.;Database=Test3;Trusted_Connection=True;");
}
}
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}
I did an add-migration m0 and then an update-database. The update-database command throws the exception:
Exception message:
The property 'PersonId1' on entity type 'ConsoleApp.Person.HomeAddress#ConsoleApp.Site' cannot be marked as nullable/optional because it has been included in a key {'PersonId1', 'PersonId2'}
Stack trace:
Applying migration '20181208175019_m0'.
System.InvalidOperationException: The property 'PersonId1' on entity type 'ConsoleApp.Person.HomeAddress#ConsoleApp.Site' cannot be marked as nullable/optional because it has been included in a key {'PersonId1', 'PersonId2'}.
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalPropertyBuilder.IsRequired(Boolean isRequired, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.IsRequired(Boolean isRequired, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.ReplaceForeignKey(InternalEntityTypeBuilder principalEntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, IReadOnlyList`1 dependentProperties, IReadOnlyList`1 oldNameDependentProperties, IReadOnlyList`1 principalProperties, Nullable`1 isUnique, Nullable`1 isRequired, Nullable`1 isOwnership, Nullable`1 deleteBehavior, Boolean removeCurrent, Boolean oldRelationshipInverted, Nullable`1 principalEndConfigurationSource, Nullable`1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.ReplaceForeignKey(Nullable`1 configurationSource, InternalEntityTypeBuilder principalEntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, Nullable`1 navigationToPrincipal, Nullable`1 navigationToDependent, IReadOnlyList`1 dependentProperties, IReadOnlyList`1 oldNameDependentProperties, IReadOnlyList`1 principalProperties, Nullable`1 isUnique, Nullable`1 isRequired, Nullable`1 isOwnership, Nullable`1 deleteBehavior, Boolean removeCurrent, Nullable`1 principalEndConfigurationSource, Boolean oldRelationshipInverted)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.Attach(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationshipSnapshot.Attach(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertiesSnapshot.Attach(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Snapshot.Attach(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity& type, String definingNavigationName, EntityType definingEntityType, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(String name, String definingNavigationName, EntityType definingEntityType, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.IsWeakTypeDefinition(ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Owns(TypeIdentity& targetEntityType, PropertyIdentity navigation, Nullable`1 inverse, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Owns(String targetEntityTypeName, String navigationName, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.OwnsOneBuilder(TypeIdentity& ownedType, String navigationName)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.OwnsOne(String ownedTypeName, String navigationName, Action`1 buildAction)
at ConsoleApp4.Migrations.m0.<>c.<BuildTargetModel>b__2_1(EntityTypeBuilder b) in C:\Users\dahum\Documents\Visual Studio 2017\Projects\ConsoleApp4\ConsoleApp4\Migrations\20181208175019_m0.Designer.cs:line 58
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
at ConsoleApp4.Migrations.m0.BuildTargetModel(ModelBuilder modelBuilder) in C:\Users\dahum\Documents\Visual Studio 2017\Projects\ConsoleApp4\ConsoleApp4\Migrations\20181208175019_m0.Designer.cs:line 34
at Microsoft.EntityFrameworkCore.Migrations.Migration.<.ctor>b__4_0()
at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Steps to reproduce
Create a new console app and copy/paste the above code or download the attached proj.
Run the next commands from package console: add-migration m0 update-database
Further technical details
EF Core version: netcoreapp2.2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2017 15.9.3 ConsoleApp4.zip
Duplicate of #14010