Dbcontext不会自动创建数据库

本文关键字:创建 数据库 Dbcontext | 更新日期: 2023-09-27 18:02:44

我正在看一个pluralsight教程,他做了下面的事情。创建了一些类,用DbContext创建了另一个类,当他试图添加一个新连接时,他可以用DbContext选择这个类。我做的完全一样,但我错过了一些东西。

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Thirty3.Models
{
     public class Thirty3Db : DbContext
    {
         public DbSet<Artist> Artists { get; set; }
         public DbSet<Record> Records { get; set; }
         public DbSet<Format> Formats { get; set; }
    }
}

想指出吗?

Dbcontext不会自动创建数据库

尝试在configuration .cs中启用AutomaticMigrationsEnabled

AutomaticMigrationsEnabled = true;

验证connectionstring并将其设置为dbContext-class构造函数。

    public Thirty3Db()
        : base("DefaultConnection") // Must match the ConnectionString name in Web.Config
    {
    }

还检查您正在连接的用户是否为SQL Server中的DB_Creator。