ASP.NET MVC添加迁移。项目未能建成

时间:2018-04-15 06:36:47

标签: asp.net-mvc

我正在尝试为数据实体启用迁移:在程序包管理器控制台中:

Enable-Migrations -ProjectName Vidly -ContextTypeName Vidly.Models.MyDBContext

我明白了:

Code First Migrations enabled for project Vidly.

然后

add-migration 'InitialModel'

我得到:"该项目' Vidly'无法建立。"

我的Configuration.cs:

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.
        }
    }
}

我的MyDBContext.cs是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models
{
    public class MyDBContext
    {
        public DbSet<Customer> Customers { get; set; } // My domain models
        public DbSet<Movie> Movies { get; set; }// My domain models
    }
}

错误消息是:

  

错误CS0311:类型&#39; Vidly.Models.MyDBContext&#39;不能用作   类型参数&#39; TContext&#39;在泛型类型或方法中   &#39; DbMigrationsConfiguration&#39 ;.没有隐含的参考   转换自&#39; Vidly.Models.MyDBContext&#39;至   &#39; System.Data.Entity.DbContext&#39;

似乎我不能在Configuration.cs中使用MyDBContext作为类型超类。 请帮忙。感谢。

3 个答案:

答案 0 :(得分:2)

您缺少实施DbContext

更改并尝试:

public class MyDBContext:DBContext
    {
        public  MyDBContext() : base("ConnectionStringName")
        {           
        }
        public DbSet<Customer> Customers { get; set; } // My domain models
        public DbSet<Movie> Movies { get; set; }// My domain models
    }

答案 1 :(得分:0)

你在这里缺少一些事情。

  1. 继承DBContext
  2. 构造函数,用于传递
  3. 中使用的连接字符串条目的名称

    使用以下代码段。注意:将VidlyDBConnectionString替换为您的连接字符串名称

        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():base("name=VidlyDBConnectionString")
                {
                 }
                public DbSet<Customer> Customers { get; set; } // My domain models
                public DbSet<Movie> Movies { get; set; }// My domain models
    
    
            }
        }
    

答案 2 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace Vidly.Models;
   {
        public class MyDBContext :DbContext
         {
            public DbSet<Customer> Customers { get; set; } // My domain models
            public DbSet<Movie> Movies { get; set; } // My domain models
         }

   }