MVC 4代码第一个数据库初始化程序不起作用

时间:2013-02-05 18:43:24

标签: 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()方法中返回一个包含存储文章列表的视图。每次调用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.

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

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:5)

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