EF 5 Enable-Migrations : No context type was found in the assembly

后端 未结 24 2010
野的像风
野的像风 2020-12-02 11:51

I have 4 projects :

Toombu.Entities : all models are there
Toombu.DataAccess: Mapping, Repository and ToombuContext
Toombu.Logique : Logic of my application         


        
相关标签:
24条回答
  • 2020-12-02 12:27

    enable-migrations -EnableAutomaticMigration:$false with this command you can enable migration at Ef 6.3 version because C# enable as default migrations at Ef 6.3 version.

    0 讨论(0)
  • 2020-12-02 12:28

    use -ProjectName option in Package Manager Console:

    Enable-Migrations -ProjectName Toombu.DataAccess -StartUpProjectName Toombu.Web -Verbose
    
    0 讨论(0)
  • 2020-12-02 12:28

    I have been getting this same problem. I have even tried above enable migrations even though I have already done. But it keeps giving same error. Then I had to use the force switch to get overcome this problem. I am sure this will help in someone else's case as well as its a possible work around.

    After enabling migration with force, you should update your database (Make sure default project is set correctly). Otherwise you will get another problem like explicit migrations are pending.

    Then just execute your add-migrations or any other commands, it should work.

    Enable-Migrations -ProjectName <PROJECT_NAME> -ContextTypeName <FULL_CONTEXT_NAMESPACE.YOUR_CONTEXT_NAME> -force
    
    0 讨论(0)
  • 2020-12-02 12:29

    Thanks for the suggestions, I solved the problem by combining all the solutions here. At first I created the DbContext Model:

     public class MyDbContext: DbContext
        {
            public MyDbContext()
            {
            }
        }
    

    After creating the dbcontext class, I ran the enable-migration command with the project Name: enable-migrations -ProjectName YourProjectName

    0 讨论(0)
  • 2020-12-02 12:30
    namespace EntityFrameworkCodeFirst.Module
    {
        public class MyDbContext: DbContext
        {
            public MyDbContext()
            {
            }
        }
    }
    

    And if you have Multiple project in one solution than you have to use below commands:-

    Enable-Migrations -ProjectName EntityFrameworkCodeFirst
    
    0 讨论(0)
  • 2020-12-02 12:31

    I created a Class in the Models directory called: myData with the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    
    namespace Vidly.Models
    {
        public class MyDbContext : DbContext
        {
            public MyDbContext()
            {
            }
        }
    }
    

    rebuilt the app with: control-shift-b

    then ran the following in the nuGet Console:

    Enable-Migrations -StartUpProjectName Vidly -ContextTypeName Vidly.Models.MyDbContext -Verbose

    the Console returned:

    Using StartUp project 'Vidly'. Using NuGet project 'Vidly'. Checking if the context targets an existing database... Code First Migrations enabled for project Vidly. Enable-Migrations -StartUpProjectName Vidly -ContextTypeName Vidly.Models.myData -Verbose

    And the FrameWork created a Migrations directory and wrote a Configuration.cs template in there with the following code:

    namespace Vidly.Migrations
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Migrations;
        using System.Linq;
    
        internal sealed class Configuration : DbMigrationsConfiguration<Vidly.Models.MyDbContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = false;
            }
    
            protected override void Seed(Vidly.Models.MyDbContext context)
            {
                //  This method will be called after migrating to the latest version.
    
                //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
                //  to avoid creating duplicate seed data.
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题