repro:
public class MyContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Offer> Offers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Offer>().HasOne(e => e.Item).WithOne().HasForeignKey("Offer", "ItemId").IsRequired();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.;Database=Repro5377;Trusted_Connection=True;MultipleActiveResultSets=true;");
}
}
public class Offer
{
public Guid Id { get; set; }
public Item Item { get; set; }
public decimal Price { get; set; }
}
public class Item
{
public Guid Id { get; set; }
}
Exception:
System.InvalidOperationException: You are configuring a relationship between 'Offer' and 'Item' but have specified a foreign key on 'Offer'. The foreign key must be defined on a type that is part of the relationship.
at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder.SetDependentEntityType(String dependentEntityTypeName) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Metadata\Builders\ReferenceReferenceBuilder.cs:line 237
at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder2.HasForeignKey(String dependentEntityTypeName, String[] foreignKeyPropertyNames) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Metadata\Builders\ReferenceReferenceBuilder
.cs:line 174
at Repro5107.MyContext.OnModelCreating(ModelBuilder modelBuilder) in D:\Projects\Repro5107\Repro5107\Program.cs:line 16
at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext dbContext) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\ModelCustomizer.cs:line 8
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\ModelSource.cs:line 81
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass14_0.
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func
2 valueFactory)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\ModelSource.cs:line 55
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Internal\DbContextServices.cs:line 52
at Microsoft.EntityFrameworkCore.Internal.LazyRef1.get_Value() in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Internal\LazyRef.cs:line 32
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Internal\DbContextServices.cs:line 65
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_4(IServiceProvider p) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\EntityFrameworkServiceCollectionExtensions.cs:line 111
at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.Storage.DatabaseProviderServices.GetService[TService]() in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Storage\DatabaseProviderServices.cs:line 57
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseProviderServices.get_Creator() in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore.SqlServer\Storage\Internal\SqlServerDatabaseProviderServices.cs:line 35
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_11(IServiceProvider p) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\EntityFrameworkServiceCollectionExtensions.cs:line 118
at Microsoft.Extensions.DependencyInjection.ServiceLookup.FactoryService.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure
1 accessor) in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\AccessorExtensions.cs:line 48
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureDeleted() in D:\k\EntityFramework\src\Microsoft.EntityFrameworkCore\Infrastructure\DatabaseFacade.cs:line 67
Note, this scenario works if OnModelCreating is changed to:
modelBuilder.Entity<Offer>().HasOne(e => e.Item).WithOne().HasForeignKey(typeof(Offer).ToString(), "ItemId").IsRequired();
but it's not obvious from the exception that one needs to specify full entity name when setting up the FK.
Most helpful comment
Note, this scenario works if OnModelCreating is changed to:
but it's not obvious from the exception that one needs to specify full entity name when setting up the FK.