2017-03-13 1 views
1

J'utilise Code-First, créer un site Web en utilisant le modèle standard, puis ajouter des tables à la principale applicationDBContext, mais maintenant quand je vais enable- dans Migration package Manager Console je reçoisCode-Tout d'abord, quand je vais enable-migrations, je reçois "Aucun type de contexte n'a été trouvé dans l'assembly 'DOC'"

Aucun type de contexte a été trouvé dans l'ensemble 'DOC'.

et quand je mets le chemin complet en utilisant:

ContextTypeName Option enable-migrations -ContextTypeName DOC.Migrations.Configuration 

... Je reçois:

ont déjà été migrations permis dans le projet 'DOC'. Pour remplacer la configuration de migrations existante, utilisez le paramètre -Force.

Et quand je vais -Force, il dit

premier code pour DOC a permis Migrations du projet.

mais lorsque je tente de add migration Je reçois

Erreur CS0311 Le type 'DOC.Migrations.Configuration' ne peut pas être utilisé comme paramètre de type 'TContext' dans le type générique ou méthode 'DbMigrationsConfiguration'. Il n'y a pas de conversion de référence implicite de 'DOC.Migrations.Configuration' à 'System.Data.Entity.DbContext'. DOC xxxxxxx \ DOC \ Migrations \ Configuration.cs.

Quand je vérifie le Configuration.cs fichier, il a changé à partir de:

internal sealed class Configuration : DbMigrationsConfiguration 

... à:

internal sealed class Configuration : DbMigrationsConfiguration 

... l'origine du projet ne parvient pas à construire

La seule fois où j'ai réussi à obtenir ce travail en déclassant le cadre d'entité de 6.1.3 à 6.1.2, puis re-mise à niveau.

Parfois, je dois mettre à niveau et déclasser quelques fois, fermer VS et rouvrir, puis cela fonctionne.

Y at-il une solution à cela? c'est vraiment agaçant.

enter image description here

J'ai aussi remarqué que ma DbContext classe dérive de IdentityDBContext, est-ce correct? Je sais habituellement que vous le dérivez de DbContext, mais depuis que j'ai démarré le projet en utilisant l'assistant, il l'a créé automatiquement et je lui ai simplement ajouté mes autres tables.

enter image description here

Mon web.config

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 

    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="data source=localhost\SQLTEST;initial catalog=DOC;User Id=xxx;Password=xxxx;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" /> 
    <!--<add name="DefaultConnection" connectionString="data source=zzzzz;initial catalog=DOC;User Id=zzzzzz;Password=zzzzz;multipleactiveresultsets=True;application name=EntityFramework" providerName="System.Data.SqlClient" />--> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <!-- 
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367. 

    The following attributes can be set on the <httpRuntime> tag. 
     <system.Web> 
     <httpRuntime targetFramework="4.5.2" /> 
     </system.Web> 
    --> 
    <system.web> 
    <authentication mode="None" /> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5" /> 
    </system.web> 
    <system.webServer> 
    <security> 
     <requestFiltering> 
     <requestLimits maxAllowedContentLength="1073741824" /> 
     </requestFiltering> 
    </security> 
    <modules> 
     <remove name="FormsAuthentication" /> 
    </modules> 
    <handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 
    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Microsoft.AspNet.Identity.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="EntityFramework.MappingAPI" publicKeyToken="7ee2e825d201459e" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.1.0.9" newVersion="6.1.0.9" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 
+0

Merci Mike pour le rangement à la question –

Répondre

0

Vous confondez les paramètres pour enable-migrations et add-migration.

enable-migrations a un nom de type de contexte. Vous spécifiez le nom de la configuration, pas le contexte.

enable-migrations -ContextTypeName DOC.Models.ApplicationDbContext 

Puis ajouter:

add-migration MyMigrationName -ConfigurationTypeName DOC.Migrations.Configuration 

Je ne suis pas sûr que vous avez même les paramètres car il semble que vous avez un seul projet. Voici le command reference

Peu importe que vous héritiez d'IdentityDbContext. De plus, EF n'a pas la version 6.3 et 6.4, c'est 6.1.3 ou EF Core.

+0

Cela ne marche pas encore, donc quand je mets la ligne suggérée, j'obtiens
Les migrations ont déjà été activées dans le projet 'DOC'. Pour remplacer la configuration de migrations existante, utilisez le paramètre -Force.
et quand je vais avec le paramètre -Force, j'obtiens
Le type 'DOC.Migrations.Configuration' n'hérite pas de 'System.Data.Entity.Migrations.DbMigrationsConfiguration'. Les types de configuration des migrations doivent s'étendre de 'System.Data.Entity.Migrations.DbMigrationsConfiguration'

et en ce qui concerne EF, désolé je voulais dire 6.1.3 –

+0

Oui, il semble que vous avez déjà activé les migrations. Vous pouvez [les réinitialiser] (https://weblog.west-wind.com/posts/2016/Jan/13/Resetting-Entity-Framework-Migrations-to-a-clean-Slate) si nécessaire. Tout est dans un projet, correct? Vous ne devriez pas avoir besoin de paramètres. 'enable-migrations' ->' migration-de-migration myNewMigration' -> 'update-database'. –

+0

Oui, tout en un projet, en essayant le test de migration supplémentaire, j'obtiens «Aucun type de configuration de migration n'a été trouvé dans l'assembly 'DOC» (Dans Visual Studio, vous pouvez utiliser la commande Enable-Migrations de Package Manager Console pour ajouter une migration configuration)." –