You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a custom type that I want to use as an entity's primary key. This type can be implicitly converted to or assigned from an int, and I've defined in int backing field in an attempt to get this to work (since using the type directly results in a different error). I should be able to use it as a backing field for a type that can be converted to/from an int.
When attempting to add migrations using my model, I get the following exception:
Exception message:
Stack trace:
System.InvalidOperationException: The specified field '_id' of type 'int' cannot be used for the property 'Cart.Id' of type 'CartKey'. Only backing fields of types that are assignable from the property type can be used.
at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.IsCompatible(FieldInfo fieldInfo, Type propertyType, Type entityClrType, String propertyName, Boolean shouldThrow)
at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.SetFieldInfo(FieldInfo fieldInfo, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalPropertyBuilder.HasField(String fieldName, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder.HasField(String fieldName)
at Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder`1.HasField(String fieldName)
at CustomKeys.Data.ApplicationDbContext.<>c.<OnModelCreating>b__1_0(EntityTypeBuilder`1 cart) in c:\Users\steve_000\Source\Repos\CustomKeys\Data\ApplicationDbContext.cs:line 27
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action`1 buildAction)
at CustomKeys.Data.ApplicationDbContext.OnModelCreating(ModelBuilder builder) in c:\Users\steve_000\Source\Repos\CustomKeys\Data\ApplicationDbContext.cs:line 25
at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
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.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.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)
The specified field '_id' of type 'int' cannot be used for the property 'Cart.Id' of type 'CartKey'. Only backing fields of types that are assignable from the property type can be used.
Steps to reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.
This is supported by type conversions (#242) in 2.1. In 2.0 it is not possible to have a mapped property of a type that the database provider doesn't support. The workaround is to not map the property and map the field only.
I have a custom type that I want to use as an entity's primary key. This type can be implicitly converted to or assigned from an
int
, and I've defined inint
backing field in an attempt to get this to work (since using the type directly results in a different error). I should be able to use it as a backing field for a type that can be converted to/from an int.When attempting to add migrations using my model, I get the following exception:
Steps to reproduce
Include a complete code listing (or project/solution) that we can run to reproduce the issue.
This is in a standard File-New-ASP.NET Core Web Application with Individual User Accounts.
Further technical details
EF Core version: 2.0.1
Database Provider: SQL Server
Operating system:
IDE: Visual Studio 2017 15.5.6
The text was updated successfully, but these errors were encountered: