我有一个使用表单身份验证的MVC4互联网应用程序。MVC4项目 - 不能DropCreateDatabaseIfModelChanges
出在Account模型文件中创建UserProfile
模型的盒子,我感动的是它自己的文件,并在我的DbContext
文件名为DbSet<UserProfile
>。我期望EF创建一个2表数据库,UserProfiles和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; }
}
用户配置型号
[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; }
}
我已经运行从包管理器启用的迁移,并允许自动迁移,并允许dataloss。生成的InitialCreate文件似乎只创建用户配置文件表,它的代码中没有任何东西表。
添加以下行的Global.asax
Database.SetInitializer(new Models.IntializeMySocialStoriesDb());
如果我现在改变对数据库的修改模型罢了。如果我手动删除数据库并将InitializeMySocialStoriesDb
更改为DropCreateDatabaseAlways,然后运行该应用程序,则不会创建数据库。
我只是想知道我错过了什么。
我想在开发期间使用EF生成所有的表结构。所以如果事情发生变化,在初始开发过程中字段会被添加等。无论如何,直到数据成为现场数据都是垃圾。 – James 2013-04-23 11:09:50
当您调试应用程序时,Visual Studio仍然会自动创建并更改表结构。您可以在SSDT中添加新字段(列),然后刷新您的EDMX。表格结构会自动改变,但仍然会改变数据。 SSDT项目也受源代码控制,因此模式也受版本控制。 – Monstieur 2013-04-23 11:11:06
我会稍微阅读一下VS中的SQL Server数据库项目(如果它只是一个单独项目中的edmx,可能没有太多东西要学习)。我在另一个项目中使用了代码优先,没有问题,但仍然有兴趣查看这次使用它出了什么问题。 – James 2013-04-23 11:16:02