我创建了一个新的MVC项目并将身份验证设置为个人用户帐户。 当我运行应用程序时,它会自动创建表以在'dbo'模式下支持这些表。 是否可以将其更改为另一个?
我已经尝试添加下面的注释行但是这不起作用 - 得到了一堆关于未定义键的错误: -
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("App1", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } //These are the lines I added - didn't work as expected. protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("MAIN"); } }I've created a new MVC project and set authentication to Individual User Accounts. When I run the application it automatically creates tables to support these under the 'dbo' schema. Is it possible to change this to another?
I've tried adding the commented lines below but this didn't work - got a bunch of errors about keys not being defined:-
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("App1", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } //These are the lines I added - didn't work as expected. protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("MAIN"); } }最满意答案
您可以对Entity Framework 6及更高版本使用modelBuilder.HasDefaultSchema("schemaName") 。
您获得IdentityDbContext原因的错误已经覆盖了OnModelCreating的DbContext并为身份实体(IdentityUser,IdentityRole等)配置了密钥。 您需要像这样调用基本方法:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("MAIN"); base.OnModelCreating(modelBuilder); // changes for identity entities here }You can use modelBuilder.HasDefaultSchema("schemaName") for Entity Framework 6 and above.
The errors you are getting its cause of IdentityDbContext already overriding the OnModelCreating of DbContext and configuring keys for Identity Entities (IdentityUser, IdentityRole etc). You need to call the base method like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("MAIN"); base.OnModelCreating(modelBuilder); // changes for identity entities here }更多推荐
发布评论