实体框架用户帐户

编程入门 行业动态 更新时间:2024-10-26 10:29:55
实体框架用户帐户 - 更改架构(Entity Framework user accounts - change schema)

我创建了一个新的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 }

更多推荐

本文发布于:2023-07-22 12:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   帐户   框架   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!