Die Fehlermeldung “Fehler beim festlegen von Datenbank-Initialisierung der Typ MyNamespace.MyCustomInitializer, MyAssembly" konnte nicht geladen MyContext geben

Ich bin erstellen einer web-app mit .NET4.5 mit Entity Framework 6 alpha3 nutzt eine neue SQL-Compact-Datenbank. Die Datenbank ist noch nicht vorhanden.

Ich habe den folgenden code in ein web-Formular:

public IQueryable<Job> listJobs_GetData()
{
    var db = new JournalistContext();
    IQueryable<Job> query = db.Jobs.Where(d => d.JobStart > DateTime.Now)
        .OrderBy(s => s.JobStart)
        .Take(10);
    return query;
}

wo die JournalistContext leitet sich von DbContext.
Es schafft die Instanz von JournalistContext ok, aber bei der Ausführung der nächsten Zeile, es wirft die exception weiter unten.
Ich vermute, da die Datenbank nicht existieren, wird versucht, rufen Sie die Initialisierung, aber dies fehlschlägt.

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=Failed to set database initializer of type 'TSJ.Models.MyCustomInitializer, TSJ' for DbContext type 'TSJ.JournalistContext, TSJ' specified in the application configuration. See inner exception for details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage)
       at System.Data.Entity.Internal.InitializerConfig.<>c__DisplayClass6.<TryGetInitializerFromEntityFrameworkSection>b__1(ContextElement e)
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
       at System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromEntityFrameworkSection(Type contextType)
       at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType)
       at System.Data.Entity.Config.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
       at System.Data.Entity.Config.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
       at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
       at System.Data.Entity.Config.AppConfigDependencyResolver.GetService(Type type, Object key)
       at System.Data.Entity.Config.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
       at System.Data.Entity.Config.ResolverChain.GetService(Type type, Object key)
       at System.Data.Entity.Config.CompositeResolver`2.GetService(Type type, Object key)
       at System.Data.Entity.Config.IDbDependencyResolverExtensions.GetService(IDbDependencyResolver resolver, Type type)
       at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
       at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
       at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
       at System.Data.Entity.Internal.InternalContext.Initialize()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
       at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
       at TSJ.MainOverview.listJobs_GetData() in ....\Visual Studio 2012\Projects\TSJ\TSJ\MainOverview.aspx.cs:line 27
  InnerException: System.TypeLoadException
       HResult=-2146233054
       Message=Could not load type 'TSJ.JournalistContext' from assembly 'TSJ'.
       Source=mscorlib
       TypeName=TSJ.JournalistContext
       StackTrace:
            at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
            at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
            at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
            at System.Type.GetType(String typeName, Boolean throwOnError)
            at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage)

Wie hier beschrieben http://msdn.microsoft.com/en-us/data/jj556606 ich habe erstellt eine benutzerdefinierte Datenbank-Initialisierung, die aktuell eine leere Klasse:

    internal sealed class MyCustomInitializer : MigrateDatabaseToLatestVersion<JournalistContext, TSJ.Migrations.Configuration>
    {

    }

    public class JournalistContext : DbContext 
    {
        public JournalistContext() : base("TSJ")
        {
        }
...

Meine web.config-Datei verweist auf diese Initialisierung wie folgt vor:

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <contexts>
      <context type="TSJ.JournalistContext, TSJ">
        <databaseInitializer type="TSJ.Models.MyCustomInitializer, TSJ" />
      </context>
    </contexts>
    <providers>
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </providers>
  </entityFramework>

Bin ich überfragt! Irgendwelche Ideen?

Andere Sache, die ich bemerkt habe: diese Zeilen haben sich in mein web.config-Datei. Ich bin mir nicht sicher, wie Sie Hinzugefügt wurden, oder warum.

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>

vielen Dank, Mark.

  • Ich habe nicht viel gearbeitet mit EF 6 - aber es scheint, es ist nicht in der Lage zu finden, Ihre Typ - oder Montage, etc. - versuchen Sie 'Kontexte' aus dem config - und manuell zu laden-Initialisierung - d.h. Datenbank.SetInitializer.
  • Gute Idee, ich setzen Sie die Zeile System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<JournalistContext, TheSportsJournalist.Migrations.Configuration>()); in meinem Application_Start-Methode und das scheint zu haben, bekam es das arbeiten. Ich habe keine Ahnung, warum es nicht funktioniert in der config-Datei aber.
  • testen, prüfen, wie Sie definieren, was ist der "voll qualifizierte name" - z.B. versuchen, den Bau, die TSJ.JournalistContext aus dem code per reflection (das ist, was config / EF zu sehen ist). Das sollte für Sie das problem. Ich kann ein paar beantworten - aber wir brauchen mehr 🙂
InformationsquelleAutor Mark Shaw | 2013-04-11
Schreibe einen Kommentar