MVC4项目-无法DropCreateDatabaseIfModelChanges [英] MVC4 Project - cannot DropCreateDatabaseIfModelChanges

查看:83
本文介绍了MVC4项目-无法DropCreateDatabaseIfModelChanges的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用表单身份验证的MVC4 Internet应用程序.

I have an MVC4 internet application using forms authentication.

现成的UserProfile模型是在帐户模型"文件中创建的,我已将其移动到它自己的文件中,并在我的DbContext文件中将其命名为>.我希望EF创建2个表的数据库,即UserProfiles和Stories.

Out of the box the UserProfile model is created in the Account models file, I have moved that to its own file and called DbSet<UserProfile> in my DbContext file. I expected EF to create a 2 table database, UserProfiles and Stories.

public class MySocialStoriesDb : DbContext
{
    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Story> Stories { get; set; }
}

故事模型

[Table("Stories")]
public class Story
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int StoryId { get; set; }

    public int UserId { get; set; }

    [ForeignKey("UserId")]
    public virtual UserProfile User { get; set; }

    public string File { get; set; }
    public DateTime DateCreated { get; set; }
    public string Name { get; set; }
}

UserProfile模型

UserProfile Model

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }
    public string EmailAddress { get; set; }
    public string UserDirectory { get; set; }
    public bool Paid { get; set; }
    public DateTime DatePaid { get; set; }
    public string PaymentId { get; set; }
}

我已经从Package Manager运行了Enable-Migrations,并允许自动迁移并允许数据丢失.生成的InitialCreate文件似乎只是在创建User Profile表,而Story表的代码中没有任何内容.

I have run Enable-Migrations from Package Manager and allowed automatic migrations and allowed dataloss. The InitialCreate file generated only seems to create the User Profile table, it doesn't have anything in its code for the Story table.

我将以下行添加到Global.asax

I added the following line to Global.asax

Database.SetInitializer(new Models.IntializeMySocialStoriesDb());

如果我现在更改模型,则数据库上的任何内容都不会更改.如果我手动删除数据库并将InitializeMySocialStoriesDb更改为DropCreateDatabaseAlways,然后运行该应用程序,则不会创建数据库.

If i now change a model nothing on the database changes. If i manually drop the database and change the InitializeMySocialStoriesDb to DropCreateDatabaseAlways and then run the app, the database is not created.

我只是想知道我错过了什么.

I am just wondering what I have missed.

推荐答案

不要理会此机制.对于生产应用程序来说,这是不可行的,并且需要您在模式每次更改时都对所有数据进行缓冲.

Don't bother with this mechanism. It's not feasible for production apps and requires you to hose all your data each time the schema changes.

相反,将数据库架构创建为一个单独的SSDT项目,并使用DACPAC文件来初始部署和升级生产数据库,同时保持所有数据不变.只需将SimpleMembership创建的表复制到您的SSDT项目中即可.

Instead create your database schema as a separate SSDT project and use DACPAC files to initially deploy and upgrade your production database while keeping all the data intact. Just copy the tables that SimpleMembership creates into your SSDT project.

通过这种方式,您可以精确地定义列,可以精确地定义约束,可以精确地定义外键,主键和唯一键等,并且仍然可以通过应用程序项目中的Entity Framework访问数据.升级生产数据库上的架构仅需要部署DACPAC.

This way you have exact column definitions, can exactly define constraints, exactly define foreign keys, primary keys and unique keys etc. and still access your data through Entity Framework in your application project. Upgrading the schema on the production database simply requires a DACPAC to be deployed.

在开发过程中,当您在SSDT中进行更改时,Visual Studio将自动对表结构进行任何更改.

During development, Visual Studio will automatically make any changes to the table structure when you make changes in SSDT.

简而言之,我认为以代码为先的定义太松散,太杂乱无章,是个坏主意.

In short I think code-first is too loosely defined, unorganised and a bad idea.

这篇关于MVC4项目-无法DropCreateDatabaseIfModelChanges的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆