efcore: model builder lambda APIs don't work with public fields on entities

repro:

        [Fact]
        public virtual void Field_key_test()
        {
            using (var ctx = new EntityWithFieldKeyContext())
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();
            }
        }

        public class EntityWithFieldKey
        {
            public long Id;
        }

        public class EntityWithFieldKeyContext : DbContext
        {
            public DbSet<EntityWithFieldKey> MyEntities { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(@"Server=.;Database=Repro_field_key;Trusted_Connection=True;MultipleActiveResultSets=True");
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<EntityWithFieldKey>().Property(e => e.Id);
                modelBuilder.Entity<EntityWithFieldKey>().HasKey(e => e.Id);
            }
        }

throws:

System.ArgumentException : The expression 'e => e.Id' is not a valid property expression. The expression should represent a simple property access: 't => t.MyProperty'.
Parameter name: propertyAccessExpression
	at Microsoft.EntityFrameworkCore.Internal.ExpressionExtensions.GetPropertyAccess(LambdaExpression propertyAccessExpression)
	at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.Property[TProperty](Expression`1 propertyExpression)
	at Microsoft.EntityFrameworkCore.Query.QueryBugsTest.EntityWithFieldKeyContext.OnModelCreating(ModelBuilder modelBuilder)
	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__1()
	at System.Lazy`1.CreateValue()
	at System.Lazy`1.LazyInitValue()
	at System.Lazy`1.get_Value()
	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, 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.Infrastructure.DatabaseFacade.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.Infrastructure.DatabaseFacade.get_DatabaseCreator()
	at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureDeleted()
	at Microsoft.EntityFrameworkCore.Query.QueryBugsTest.Field_key_test()

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 24 (24 by maintainers)

Most upvoted comments

@ilmax - Sorry that you are having difficulties in getting dev env working to contribute. Given where master branch is, using preview .net core would be necessary. But I haven’t seen any VS crash so far. Can you describe steps you have used in order to use preview SDK in VS? Further there are multiple ways to run single test.

  • VS Test explorer (It works for me in 15.9 Preview version at least)
  • Test Driven (It is bit manual to set it up but it is the best experience you can get.
  • Dotnet test with a variety of command line args can make single test run and perhaps debug in VS too.

Let me know any way we can help you out.

Note: thanks to @ilmax for the initial PR on this: #11610 . This PR was based on that one.

@smitpatel, @ajcvickers removing the %userprofile%/.dotnet did the trick, still unsure how the command line was able to build it succesfully, but this is another story. Thank you for the tips

@ilmax My workflow to get started again after doing other things for a few days is:

  • Clear my NuGet caches. (Always good to do this periodically when working with pre-release bits, but we rarely see the issues we had in the past.)
  • git clean -xdf on the repo clone
  • build /t:Package to ensure the preview runtime is installed and get the build working
  • Open the solution in my favorite IDE and go party

@divega I would like to give this a try