【问题标题】:MVC 4 code first database initializer doesn't workMVC 4 代码优先数据库初始化程序不起作用
【发布时间】:2013-01-20 19:06:11
【问题描述】:

我不得不在不同的 MVC 4 代码优先技术教程的相同阶段停下来,因为数据库初始化失败。

使用连接

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-DbTestApp-20130205173443;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-DbTestApp-20130205173443.mdf" providerName="System.Data.SqlClient" />

我什至无法创建或管理我想从我的模型中生成的数据库

public class Category
    {
        public int Id { get; set; }

        [Required]
        [MinLength(4)]
        [MaxLength(64)]
        public string Name { get; set; }

        public virtual IEnumerable<Article> Articles { get; set; }
    }

    public class Article
    {
        public int Id { get; set; }

        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }

        [Required]
        [MinLength(4)]
        [MaxLength(64)]
        public string Title { get; set; }

        [Required]
        [MinLength(16)]
        [MaxLength(1024)]
        [DataType(DataType.MultilineText)]
        public string Content { get; set; }

        [Required]
        [Display(Name = "Post Anonymous?")]
        public bool IsAnonymous { get; set; }

        public int AuthorId { get; set; }
        public virtual Author Author { get; set; }

        public virtual IEnumerable<Comment> Comments { get; set; }

    }

    public class Author
    {
        public int Id { get; set; }

        [MinLength(3)]
        [MaxLength(64)]
        public string AuthorName { get; set; }

        public virtual IEnumerable<Article> Articles { get; set; }

        public virtual IEnumerable<Category> Categories { get; set; }
    }

    public class Comment
    {
        public int Id { get; set; }

        public int ArticleId { get; set; }
        public virtual Article Article { get; set; }

        [Required]
        [MinLength(3)]
        [MaxLength(64)]
        public string Author { get; set; }

        [MaxLength(64)]
        public string Title { get; set; }

        [Required]
        [MinLength(4)]
        [MaxLength(512)]
        public string Content { get; set; }
    }

使用下面的上下文

    public class BlogContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Article> Articles { get; set; }
        public DbSet<Author> Authors { get; set; }
        public DbSet<Comment> Comments { get; set; }

        public BlogContext()
            : base("DefaultConnection")
        {
            Configuration.ProxyCreationEnabled = false;
        }
    }

我还在 Global.asax Application_Start() 方法中设置了初始化器:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BlogContext>());

当我试图打电话时问题来了

var articles = db.Articles.Include(a => a.Category).Include(a => a.Author);

在我的 BlogController 的 Index() 方法中返回一个包含已存储文章列表的视图。每次调用数据库相关方法时都会发生这种情况,错误消息是:

Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.

在教程中我没有发现类似的问题,我阅读的解决方案也无法解决问题。

有什么想法吗?

谢谢

【问题讨论】:

    标签: entity-framework asp.net-mvc-4 ef-code-first


    【解决方案1】:

    DropCreateDatabaseIfModelChanges 要求在比较两者之前已经存在以前的模型。要启动数据库,您需要使用 DropCreateDatabaseAlways 初始化程序。

    【讨论】:

    • 我也试过了,报错:Cannot drop database "aspnet-DbTestApp-20130205173443" because it is current in use.
    • SQL Server Management Studio 是否仍然打开?因为这将使与该数据库的连接保持打开状态。
    • 不,您需要将其关闭。 SQL Server 保持与该数据库的连接。为了让您删除数据库,您需要关闭 SQL Server 以关闭连接。
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2015-07-24
    • 2013-07-04
    • 2018-09-27
    相关资源
    最近更新 更多