1

I want to find exceptions that may influence my c# winforms xaf application load time. So I turned on Common Language Runtime Settings

Now I experience this error

  System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Invalid object name 'dbo.EdmMetadata'.
  Source=.Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

This exception was originally thrown at this call stack: System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException, bool, System.Action<System.Action>)

Error message

The code is

    protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args)
    {
        if (args.Connection == null)
        {

            // The error occurs here
            var provider =
                new EFObjectSpaceProvider(typeof(MyAppDbContext), TypesInfo, null, args.ConnectionString);  
            args.ObjectSpaceProviders.Add(provider);
        }
        else
        {
            args.ObjectSpaceProviders.Add(new EFObjectSpaceProvider(typeof(MyAppDbContext), TypesInfo, null,
                (DbConnection) args.Connection));
        }

       
        args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
    }

I am using initializers in my DBContext as advised in this answer

My DbContext looks like

public class MyAppDbContext : DbContext
{
    public MyAppDbContext()
        : base("name=ApplicationMyApp")
    {
        Database.SetInitializer<MyAppDbContext>(null);
    }

    public MyAppDbContext(string connectionString)
        : base(connectionString)
    {
        Database.SetInitializer<MyAppDbContext>(null);
    }

    public MyAppDbContext(DbConnection connection)
        : base(connection, false)
    {
         Database.SetInitializer<MyAppDbContext>(null);
    }

    public MyAppDbContext(ObjectContext objectContext)
        : base(objectContext, false)
    {
       Database.SetInitializer<MyAppDbContext>(null);
    }
   // etc

I am using .Net Framework 4.7.2 with Entity Framework 6.4.4

The call stack is

System.Data.dll!System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException exception, bool breakConnection, System.Action<System.Action> wrapCloseInAction)
System.Data.dll!System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject stateObj, bool callerHasConnectionLock, bool asyncClose)
System.Data.dll!System.Data.SqlClient.TdsParser.TryRun(System.Data.SqlClient.RunBehavior runBehavior, System.Data.SqlClient.SqlCommand cmdHandler, System.Data.SqlClient.SqlDataReader dataStream, System.Data.SqlClient.BulkCopySimpleResultSet bulkCopyHandler, System.Data.SqlClient.TdsParserStateObject stateObj, out bool dataReady)
System.Data.dll!System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
System.Data.dll!System.Data.SqlClient.SqlDataReader.MetaData.get()
System.Data.dll!System.Data.SqlClient.SqlCommand.FinishExecuteReader(System.Data.SqlClient.SqlDataReader ds, System.Data.SqlClient.RunBehavior runBehavior, string resetOptionsString, bool isInternal, bool forDescribeParameterEncryption, bool shouldCacheForAlwaysEncrypted)
System.Data.dll!System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(System.Data.CommandBehavior cmdBehavior, System.Data.SqlClient.RunBehavior runBehavior, bool returnStream, bool async, int timeout, out System.Threading.Tasks.Task task, bool asyncWrite, bool inRetry, System.Data.SqlClient.SqlDataReader ds, bool describeParameterEncryptionRequest)
System.Data.dll!System.Data.SqlClient.SqlCommand.RunExecuteReader(System.Data.CommandBehavior cmdBehavior, System.Data.SqlClient.RunBehavior runBehavior, bool returnStream, string method, System.Threading.Tasks.TaskCompletionSource<object> completion, int timeout, out System.Threading.Tasks.Task task, out bool usedCache, bool asyncWrite, bool inRetry)
System.Data.dll!System.Data.SqlClient.SqlCommand.RunExecuteReader(System.Data.CommandBehavior cmdBehavior, System.Data.SqlClient.RunBehavior runBehavior, bool returnStream, string method)
System.Data.dll!System.Data.SqlClient.SqlCommand.ExecuteReader(System.Data.CommandBehavior behavior, string method)
EntityFramework.dll!System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader.AnonymousMethod__6_0(System.Data.Common.DbCommand t, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<System.Data.Common.DbDataReader> c)
EntityFramework.dll!System.Data.Entity.Infrastructure.Interception.InternalDispatcher<System.Data.Entity.Infrastructure.Interception.IDbCommandInterceptor>.Dispatch<System.Data.Common.DbCommand, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<System.Data.Common.DbDataReader>, System.Data.Common.DbDataReader>(System.Data.Common.DbCommand target, System.Func<System.Data.Common.DbCommand, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<System.Data.Common.DbDataReader>, System.Data.Common.DbDataReader> operation, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext, System.Action<System.Data.Entity.Infrastructure.Interception.IDbCommandInterceptor, System.Data.Common.DbCommand, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<System.Data.Common.DbDataReader>> executing, System.Action<System.Data.Entity.Infrastructure.Interception.IDbCommandInterceptor, System.Data.Common.DbCommand, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext<System.Data.Common.DbDataReader>> executed)
EntityFramework.dll!System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(System.Data.Common.DbCommand command, System.Data.Entity.Infrastructure.Interception.DbCommandInterceptionContext interceptionContext)
EntityFramework.dll!System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(System.Data.CommandBehavior behavior)
EntityFramework.dll!System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(System.Data.Entity.Core.EntityClient.EntityCommand entityCommand, System.Data.CommandBehavior behavior)
EntityFramework.dll!System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute<System.Data.Entity.Infrastructure.EdmMetadata>(System.Data.Entity.Core.Objects.ObjectContext context, System.Data.Entity.Core.Objects.ObjectParameterCollection parameterValues)
EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<System.Data.Entity.Infrastructure.EdmMetadata>.GetResults.AnonymousMethod__1()
EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction<System.Data.Entity.Core.Objects.ObjectResult<System.Data.Entity.Infrastructure.EdmMetadata>>(System.Func<System.Data.Entity.Core.Objects.ObjectResult<System.Data.Entity.Infrastructure.EdmMetadata>> func, System.Data.Entity.Infrastructure.IDbExecutionStrategy executionStrategy, bool startLocalTransaction, bool releaseConnectionOnSuccess)
EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<System.Data.Entity.Infrastructure.EdmMetadata>.GetResults.AnonymousMethod__0()
EntityFramework.SqlServer.dll!System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute<System.Data.Entity.Core.Objects.ObjectResult<System.Data.Entity.Infrastructure.EdmMetadata>>(System.Func<System.Data.Entity.Core.Objects.ObjectResult<System.Data.Entity.Infrastructure.EdmMetadata>> operation)
EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<System.Data.Entity.Infrastructure.EdmMetadata>.GetResults(System.Data.Entity.Core.Objects.MergeOption? forMergeOption)
EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<System.Data.Entity.Infrastructure.EdmMetadata>.System.Collections.Generic.IEnumerable<T>.GetEnumerator.AnonymousMethod__31_0()
EntityFramework.dll!System.Data.Entity.Internal.LazyEnumerator<System.Data.Entity.Infrastructure.EdmMetadata>.MoveNext()
System.Core.dll!System.Linq.Enumerable.FirstOrDefault<System.Data.Entity.Infrastructure.EdmMetadata>(System.Collections.Generic.IEnumerable<System.Data.Entity.Infrastructure.EdmMetadata> source)
EntityFramework.dll!System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.GetElementFunction.AnonymousMethod__14_1(System.Collections.Generic.IEnumerable<System.Data.Entity.Infrastructure.EdmMetadata> sequence)
EntityFramework.dll!System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle<System.Data.Entity.Infrastructure.EdmMetadata>(System.Collections.Generic.IEnumerable<System.Data.Entity.Infrastructure.EdmMetadata> query, System.Linq.Expressions.Expression queryRoot)
EntityFramework.dll!System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.Execute<System.Data.Entity.Infrastructure.EdmMetadata>(System.Linq.Expressions.Expression expression)
EntityFramework.dll!System.Data.Entity.Internal.Linq.DbQueryProvider.Execute<System.Data.Entity.Infrastructure.EdmMetadata>(System.Linq.Expressions.Expression expression)
EntityFramework.dll!System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(System.Func<System.Data.Common.DbConnection, System.Data.Entity.Internal.EdmMetadataContext> createContext)
EntityFramework.dll!System.Data.Entity.Internal.InternalContext.QueryForModelHash()
EntityFramework.dll!System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(System.Data.Entity.Internal.InternalContext internalContext, System.Data.Entity.Internal.ModelHashCalculator modelHashCalculator, bool throwIfNoMetadata, System.Data.Entity.Internal.DatabaseExistenceState existenceState)
EntityFramework.dll!System.Data.Entity.Internal.InternalContext.CompatibleWithModel(bool throwIfNoMetadata, System.Data.Entity.Internal.DatabaseExistenceState existenceState)
EntityFramework.dll!System.Data.Entity.Database.CompatibleWithModel(bool throwIfNoMetadata, System.Data.Entity.Internal.DatabaseExistenceState existenceState)
EntityFramework.dll!System.Data.Entity.Database.CompatibleWithModel(bool throwIfNoMetadata)
DevExpress.ExpressApp.EF.v21.1.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CheckDatabaseSchemaCompatibilityCore(object context)
DevExpress.ExpressApp.EF.v21.1.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.CreateObjectContext(System.Collections.Generic.IList<System.IDisposable> disposableObjects)
DevExpress.ExpressApp.EF.v21.1.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.Init(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, System.Data.Common.DbConnection connection, string connectionString, string metadataLocations, string providerName)
DevExpress.ExpressApp.EF.v21.1.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.EFObjectSpaceProvider(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, string connectionString, string metadataLocations, string providerName)
DevExpress.ExpressApp.EF.v21.1.dll!DevExpress.ExpressApp.EF.EFObjectSpaceProvider.EFObjectSpaceProvider(System.Type contextType, DevExpress.ExpressApp.DC.ITypesInfo typesInfo, DevExpress.ExpressApp.EF.EFTypeInfoSource typeInfoSource, string connectionString)
VIV.MyApp2.Win.exe!MyApp2.Win.MyApp2WindowsFormsApplication.CreateDefaultObjectSpaceProvider(DevExpress.ExpressApp.CreateCustomObjectSpaceProviderEventArgs args) Line 42
    at D:\dev\Viv.MyApp2\MyApp2.Win\WinApplication.cs(42)
DevExpress.ExpressApp.v21.1.dll!DevExpress.ExpressApp.XafApplication.CreateObjectSpaceProviders(string connectionString)
DevExpress.ExpressApp.v21.1.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, string connectionString, string[] moduleAssemblies, DevExpress.ExpressApp.Security.ISecurityStrategyBase security)
DevExpress.ExpressApp.v21.1.dll!DevExpress.ExpressApp.XafApplication.Setup()
VIV.MyApp2.Win.exe!MyApp2.Win.Program.Main() Line 49
    at D:\dev\Viv.MyApp2\MyApp2.Win\Program.cs(49)

[Update] The following looked promising but it did not help.

            var provider =
                new EFObjectSpaceProvider(typeof(JobTalkDbContext), TypesInfo, null, args.ConnectionString)
                {
                    SchemaUpdateMode = SchemaUpdateMode.None
                }; //errors here

TypeInfoSource

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • Checking ~https://stackoverflow.com/questions/5373702/why-does-entity-framework-code-first-with-an-existing-db-keep-trying-get-data – Kirsten Jun 19 '21 at 21:09

1 Answers1

0

This error generally throws when your db does not exists in your SQL server. Kindly, check your db on the configured connection.