I am trying to set the primary key constraint name of the parent of an owned type and I am getting an exception:
Exception type: InvalidOperationException
Exception message: Cannot use table 'Orders' for entity type 'Order' since it is being used for entity type 'Order.OrderDetails#OrderDetails' and the name 'PK_OrderKey' of the primary key {'Id'} does not match the name 'PK_Orders' of the primary key {'PK_OrderKey'}.
Stack trace:
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateSharedTableCompatibility(IReadOnlyList`1 mappedTypes, String tableName)
at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.ValidateSharedTableCompatibility(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.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__0(Object k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
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_1(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.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.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureDeleted()
at EFCore2Samples.Program.Main(String[] args) in C:\Users\chaws\source\repos\EFCore2Samples\EFCore2Samples\Program.cs:line 17
Using the following code in a new console application will replicate the issue. Note that if you remove .HasName("PK_OrderKey");
then the application will run without issue.
```c#
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EFCore2Samples
{
class Program
{
static void Main(String[] args)
{
using (var db = new OrderContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.Orders.Add(new Order
{
OrderNumber = "PO0001",
OrderDetails = new OrderDetails
{
BillingAddress = new StreetAddress
{
City = "City 1",
Street = "Street 1"
},
ShippingAddress = new StreetAddress
{
City = "City 2",
Street = "Street 2"
}
}
});
db.SaveChanges();
}
Console.ReadKey();
}
}
public class OrderContext : DbContext
{
public DbSet<Order> Orders { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("Server=(localdb)\\mssqllocaldb; Database=EfTest");
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Order>(entity =>
{
// If I take out the .HasName("PK_OrderKey"); this work as expected.
entity.HasKey(x => x.Id).HasName("PK_OrderKey");
entity.OwnsOne(p => p.OrderDetails, cb =>
{
cb.OwnsOne(c => c.BillingAddress, billing =>
{
billing.Property(x => x.City).HasColumnName("BillingCity");
billing.Property(x => x.Street).HasColumnName("BillingStreet");
});
cb.OwnsOne(c => c.ShippingAddress, shipping =>
{
shipping.Property(x => x.City).HasColumnName("ShippingCity");
shipping.Property(x => x.Street).HasColumnName("ShippingStreet");
});
});
});
}
}
public class Order
{
public Int32 Id { get; set; }
public String OrderNumber { get; set; }
public OrderDetails OrderDetails { get; set; }
}
public class OrderDetails
{
public StreetAddress BillingAddress { get; set; }
public StreetAddress ShippingAddress { get; set; }
}
public class StreetAddress
{
public String Street { get; set; }
public String City { get; set; }
}
}
```
EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.0.0
Operating system: Windows 10 Redstone 3
IDE: Visual Studio 2017 (15.4.0 Preview 1.0)
@ChristopherHaws This code:
```C#
entity.HasKey(x => x.Id).HasName("OrderId");
sets the name of the primary key constraint in the database. This should still work for owned types, so keeping this bug open, but if you want to change the column name, then use:
```C#
entity.Property(x => x.Id).HasColumnName("OrderId");
This worked for me when I tested on 2.0.0.
@ajcvickers Thanks for that. I figured I was doing something wrong. I modified the original post to reflect the actual bug of not being able to set the primary key constraint name.
Note, see #10308 and consider all facets when fixing this.
Most helpful comment
@ChristopherHaws This code:
```C#
entity.HasKey(x => x.Id).HasName("OrderId");
This worked for me when I tested on 2.0.0.