entity-framework - MVC 4 代码优先数据库初始化程序不起作用

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

我不得不在遵循不同的 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() 方法中返回包含存储文章列表的 View 。每次调用DB相关方法都会出现这种情况,错误信息是:

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.

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

有什么想法吗?

谢谢

最佳答案

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

关于entity-framework - MVC 4 代码优先数据库初始化程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14714581/

相关文章:

c# - Entity Framework 代码优先主键约束名称

c# - 使用继承模型进行实体拆分

entity-framework-4 - Entity Framework 中的代码优先实现

c# - 我怎样才能得到linq中的计数?

.net - 向 ADO .NET Entity Framework 添加业务层

sql-server - 多个实例的 Entity Framework 迁移策略

c# - LINQ 等效的 MYSQL 查询

c# - 如何创建嵌套的 GridView 来编辑 EF Code First 关系?

javascript - 如何将参数传递给 LI 中的 onclick ="window.location"

asp.net-mvc-4 - 存储用户配置设置的最佳位置是什么