微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实体框架 – ASP.NET Identity Model First因重命名的AspNetUserRoles列而失败

像其他几个一样,我试图实现ASP.NET Identity Model First.一旦你尝试,错误,气氛,搜索解决,一切都很好.我想.

也可以看看:

> ASP.NET Identity with EF Database First MVC5
> http://danieleagle.com/blog/2014/05/setting-up-asp-net-identity-framework-2-0-with-database-first-vs2013-update-2-spa-template/

行动方案,总结如下:

>创建认项目(MVC5)
>创建数据库
>更新web.config中的connectionstring
>运行网站,注册:表格已创建
>创建EF模型(edmx)
>导入身份表(到目前为止一切都很好)
>修改了xxx.Context.tt以继承IdentityDbContext
>生成数据库脚本(麻烦从这里开始)

我已经解决了出现的问题(直到最新一步).为了完整起见,我将描述它们.

使用认的标识上下文

一切正常:表格创建,我可以注册登录.然而,这不是我想要的情况:我想使用Model First方法.

使用EF连接字符串使用自定义的第一个上下文模型

修改CreatePerOwinContext,使其使用我的Model First上下文:

public void ConfigureAuth(IAppBuilder app)
{
    app.CreatePerOwinContext(CustomModelFirstDbContext.Create);

和ApplicationUserManager一起使用Model First上下文:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,IOwinContext context) 
{
    var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<CustomModelFirstDbContext>()));

结果是:

Server Error in ‘/’ Application.

The entity type ApplicationUser is not part of the model for the
current context.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: system.invalidOperationException: The entity type
ApplicationUser is not part of the model for the current context.

Source Error:

An unhandled exception was generated during the execution of the
current web request. information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:
[InvalidOperationException: The entity type ApplicationUser is not
part of the model for the current context.]

将“普通”连接字符串与自定义的Model First上下文一起使用

An exception of type
‘System.Data.Entity.Infrastructure.UnintentionalCodeFirstException’
occurred in WebApplication1.dll but was not handled in user code

Additional information: Code generated using the T4 templates for
Database First and Model First development may not work correctly if
used in Code First mode. To continue using Database First or Model
First ensure that the Entity Framework connection string is specified
in the config file of executing application. To use these classes,
that were generated from Database First or Model First,with Code
First add any additional configuration using attributes or the
DbModelBuilder API and then remove the code that throws this
exception.

所以,我认为我需要认的Identity上下文来使用Identity,并使用自定义的Model First上下文来处理其他所有内容.不是首选的解决方案,但可以接受.

>回滚所有东西
>从数据库导入身份表
>(可选)通过Model First方法创建实体
>生成数据库脚本

正常项目和快速健全性检查测试项目都与AspNetUserRoles表具有相同的问题.这是一个联结表,当在EF设计器中导入它时,一切正常.你不会看到它,因为它是一个多对多的关系,当检查AspNetRole和AspNetUser之间的关联时它看起来不错.

设计师和制图细节:

但是,在生成sql脚本时,EF会修改密钥.

设计师和制图细节:

生成sql脚本:

-- Creating table 'AspNetUserRoles'
CREATE TABLE [dbo].[AspNetUserRoles] (
[AspNetRoles_Id] nvarchar(128) NOT NULL,[AspNetUsers_Id] nvarchar(128) NOT NULL
);
GO

在EF中,您无法更改设计器中映射的名称(thread on social.msdn.microsoft.com).

随后,使用最初创建的上下文创建新用户将失败,因为联结表包含错误的列:

Server Error in ‘/’ Application.

Invalid column name ‘UserId’.

Invalid column name ‘UserId’.

Invalid column name ‘UserId’.

Invalid column name ‘RoleId’.

Invalid column name ‘UserId’.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Data.sqlClient.sqlException: Invalid column name ‘UserId’. Invalid column name ‘UserId’. Invalid column name ‘UserId’. Invalid column name ‘RoleId’. Invalid column name ‘UserId’.

Source Error:

Line 89: {

Line 90: var user = new ApplicationUser() { UserName = model.Email,Email = model.Email };

Line 91: IdentityResult result = await UserManager.CreateAsync(user,model.Password);

Line 92: if (result.Succeeded)

Line 93: {

解决办法是什么?除了尝试更改生成的脚本或转移到Code First之外,还有其他选择吗?

解决方法

如果你在begginning和db仍然是空的
我相信最简单的解决方法是:

>创建EF模型(edmx).
>右键单击“从模型生成数据库”模型.
>它将创建DDL文件(下面的代码段)
>替换所有错误的“AspNetUsers_Id”和“AspNetRoles_Id”以获取正确的值.
>右键单击“执行”.

适合我.

– 为FOREIGN KEY’FK_AspNetUserRoles_AspNetUser’创建非聚集索引
CREATE INDEX [IX_FK_AspNetUserRoles_AspNetUser]
ON [dbo].[AspNetUserRoles]
([AspNetUsers_Id]); //替换UserId

快乐的编码!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐