voronov-maxim / OdataToEntity

OData .net core
MIT License
153 stars 32 forks source link

Creating OData API via "OdataToEntity.EfCore.DynamicDataContext" #48

Closed piyushparate1 closed 4 years ago

piyushparate1 commented 4 years ago

Hi!

I am trying to create OData API via referring "OdataToEntity.EfCore.DynamicDataContext" project in my main .net core web application project.

Below is the Startup.cs Configure function:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
            var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
            optionsBuilder = optionsBuilder.UseSqlServer("Server=tcmy****dbsvr.database.windows.net,1433;Initial Catalog=my*****db;Persist Security Info=False;User ID=*****;Password=****;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
            using (ProviderSpecificSchema providerSchema = new SqlServerSchema(optionsBuilder.Options))
            {
                IEdmModel edmModel = DynamicMiddlewareHelper.CreateEdmModel(providerSchema, informationSchemaMapping: null);
                app.UseOdataToEntityMiddleware<OePageMiddleware>("/api", edmModel);
            }
        }

But on running the project, it throwing me error in below code:

namespace OdataToEntity.EfCore.DynamicDataContext.ModelBuilder class DynamicModelBuilder private EntityType CreateEntityType(***) Line: entityTypeBuilder.Metadata.IsKeyless = true;

System.InvalidOperationException HResult=0x80131509 Message=The entity type 'DynamicType1' cannot be marked as keyless because it contains a key. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.HasNoKey(Nullable1 keyless, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.set_IsKeyless(Boolean value) at OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicModelBuilder.CreateEntityType(ModelBuilder modelBuilder, String tableEdmName, Boolean isQueryType) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\ModelBuilder\DynamicModelBuilder.cs:line 65 at OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicModelBuilder.Build(ModelBuilder modelBuilder) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\ModelBuilder\DynamicModelBuilder.cs:line 28 at OdataToEntity.EfCore.DynamicDataContext.DynamicDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\DynamicDbContext.cs:line 26 at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>cDisplayClass1_0.b0(ServiceProviderEngineScope scope) 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.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at OdataToEntity.EfCore.DynamicDataContext.DynamicTypeDefinitionManager.Create(DynamicMetadataProvider metadataProvider) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\DynamicTypeDefinitionManager.cs:line 55 at OdataToEntity.EfCore.DynamicDataContext.DynamicMiddlewareHelper.CreateEdmModel(ProviderSpecificSchema providerSchema, InformationSchemaMapping informationSchemaMapping) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\DynamicMiddlewareHelper.cs:line 12 at WebApplication1.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\OdataToEntity\sln\WebApplication1\Startup.cs:line 59 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>cDisplayClass4_0.b0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>cDisplayClass13_0.b2(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>cDisplayClass0_0.gMiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>cDisplayClass2_0.b0(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>cDisplayClass0_0.b0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.d__31.MoveNext()

voronov-maxim commented 4 years ago

To reproduce this error, I need a database schema script like this.

piyushparate1 commented 4 years ago

Thanks for your reply! Attached the database schema script.

MyNorthwind.zip

piyushparate1 commented 4 years ago

To reproduce this error, I need a database schema script like this.

i tries to deploy this script db schema as well! throwing the same kind of issue:

System.InvalidOperationException HResult=0x80131509 Message=The entity type 'DynamicType1' cannot be marked as keyless because it contains a key. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.HasNoKey(Nullable1 keyless, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.set_IsKeyless(Boolean value) at OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicModelBuilder.CreateEntityType(ModelBuilder modelBuilder, String tableEdmName, Boolean isQueryType) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\ModelBuilder\DynamicModelBuilder.cs:line 65 at OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicModelBuilder.Build(ModelBuilder modelBuilder) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\ModelBuilder\DynamicModelBuilder.cs:line 28 at OdataToEntity.EfCore.DynamicDataContext.DynamicDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\DynamicDbContext.cs:line 26 at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>cDisplayClass1_0.b0(ServiceProviderEngineScope scope) 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.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at OdataToEntity.EfCore.DynamicDataContext.DynamicTypeDefinitionManager.Create(DynamicMetadataProvider metadataProvider) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\DynamicTypeDefinitionManager.cs:line 55 at OdataToEntity.EfCore.DynamicDataContext.DynamicMiddlewareHelper.CreateEdmModel(ProviderSpecificSchema providerSchema, InformationSchemaMapping informationSchemaMapping) in C:\OdataToEntity\source\OdataToEntity.EfCore.DynamicDataContext\DynamicMiddlewareHelper.cs:line 12 at WebApplication1.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\OdataToEntity\sln\WebApplication1\Startup.cs:line 59 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>cDisplayClass4_0.b0(IApplicationBuilder builder) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>cDisplayClass13_0.b2(IApplicationBuilder app) at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>cDisplayClass0_0.gMiddlewareFilterBuilder|0(IApplicationBuilder builder) at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>cDisplayClass2_0.b0(IApplicationBuilder app) at Microsoft.AspNetCore.HostFilteringStartupFilter.<>cDisplayClass0_0.b0(IApplicationBuilder app) at Microsoft.AspNetCore.Hosting.GenericWebHostService.d31.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() at Microsoft.Extensions.Hosting.Internal.Host.d9.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.d4.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.d4.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) at WebApplication1.Program.Main(String[] args) in C:\OdataToEntity\sln\WebApplication1\Program.cs:line 16

This exception was originally thrown at this call stack: [External Code] OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicModelBuilder.CreateEntityType(Microsoft.EntityFrameworkCore.ModelBuilder, string, bool) in DynamicModelBuilder.cs OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicModelBuilder.Build(Microsoft.EntityFrameworkCore.ModelBuilder) in DynamicModelBuilder.cs OdataToEntity.EfCore.DynamicDataContext.DynamicDbContext.OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder) in DynamicDbContext.cs [External Code] OdataToEntity.EfCore.DynamicDataContext.DynamicTypeDefinitionManager.Create(OdataToEntity.EfCore.DynamicDataContext.ModelBuilder.DynamicMetadataProvider) in DynamicTypeDefinitionManager.cs OdataToEntity.EfCore.DynamicDataContext.DynamicMiddlewareHelper.CreateEdmModel(OdataToEntity.EfCore.DynamicDataContext.InformationSchema.ProviderSpecificSchema, OdataToEntity.EfCore.DynamicDataContext.InformationSchema.InformationSchemaMapping) in DynamicMiddlewareHelper.cs WebApplication1.Startup.Configure(Microsoft.AspNetCore.Builder.IApplicationBuilder, Microsoft.AspNetCore.Hosting.IWebHostEnvironment) in Startup.cs [External Code] WebApplication1.Program.Main(string[]) in Program.cs