ASP。NET MVC 5首先与模型一致

时间:2022-12-20 21:03:32

I'm developing a web application and I would like to implement Identity. I've done a couple of tests and it's pretty easy to implement when using a Code First approach.

我正在开发一个web应用程序,我想实现Identity。我已经做了一些测试,当使用代码优先方法时,它很容易实现。

My issue is that I can't get it to work with a Model First approach. I would like to define my models in the .edmx file, and generate my database using the "Generate database from model" option.

我的问题是,我不能让它以模型优先的方式工作。我想在.edmx文件中定义模型,并使用“从模型生成数据库”选项生成数据库。

Here's what I've done:

这是我所做的:

I started from the default ASP .NET Web Application template, including the "Individual User Account" authentication. Then I registered a user in order for entity framework to generate the needed tables in the default database. Then I added the "ADO .NET Entity Data Model" to my project, and chose the "EF Designer from Database" option. It has been generated successfully with the existing tables created by Identity. I changed the connection string in the IdentityModels:

我从默认的asp.net Web应用程序模板开始,包括“个人用户帐户”身份验证。然后我注册了一个用户,以便让实体框架在默认数据库中生成所需的表。然后我将“ADO . net实体数据模型”添加到我的项目中,并选择了“EF Designer from Database”选项。它已经通过标识创建的现有表成功生成。我在IdentityModels中更改了连接字符串:

public ApplicationDbContext()
        : base("MyConnectionStringName", throwIfV1Schema: false)
{
}

But after this, when I try to register a user, I get the following error:

但在此之后,当我尝试注册一个用户时,我得到了以下错误:

"Unable to load the specified metadata resource."

“无法加载指定的元数据资源。”

Previously, I also had an error: "The entity type ApplicationUser is not part of the model for the current context."

之前,我还犯了一个错误:“实体类型ApplicationUser不是当前上下文的模型的一部分。”

Is it actually possible to use Identity with a Model First approach ? If yes, what am I doing wrong ?

用模型第一的方法来使用标识,真的可能吗?如果是,我做错了什么?

1 个解决方案

#1


3  

I said I'll post something in order to explain how I managed to get this working. I struggled to find some documentation and tutorials about using Identity with a Model First approach, so here it comes.

我说过我会发布一些东西来解释我是如何让它工作的。我很难找到一些关于使用模型第一方法的标识的文档和教程,所以它出现了。

Firstly, I found this genius article from Ben Foster: http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

首先,我从Ben Foster那里找到了这篇天才的文章:http://benfoster.io/blog/aspnet-id -stripped- baremail - part1。

This article discuss about how to implement an "Identity Stripped Bare". Reading it and following the steps allowed me understand how to use identity from scratch (you can see at the beggining that he generates an MVC 5 project with no authentication system). If you want the code of this "Naked Identity", here it is: https://github.com/benfoster/NakedIdentity

这篇文章讨论了如何实现一个“剥去的身份”。阅读它并遵循这些步骤让我了解如何从头开始使用标识(您可以看到,他生成了一个没有身份验证系统的MVC 5项目)。如果您想要这个“裸身份”的代码,这里是:https://github.com/benfoster/NakedIdentity

The first step was to generate the database. In the Naked Identity code, the v1.0 of Identity is used. Some things are different (a couple table properties for example), but it stays mostly identical. To create a database usable by Identity, I simply ran the template MVC 5 project, registered a user for the table to be created with the Code First approach, and then copied those tables needed by Identity in my empty database.

第一步是生成数据库。在Naked Identity代码中,使用了身份的v1.0。有些东西是不同的(例如一些表属性),但是它们基本上是相同的。要创建一个通过标识可用的数据库,我只需运行模板MVC 5项目,为使用代码优先方法创建的表注册一个用户,然后在我的空数据库中复制标识所需的表。

Once this was done, I generated my .edmx using the freshly created database. A connection string is added to the web.config. A connection string may already exist as "DefaultConnection". You need to keep this one as well.

完成此操作后,我使用新创建的数据库生成了.edmx。一个连接字符串被添加到web.config中。连接字符串可能已经作为“DefaultConnection”存在。你也需要保留这个。

Basically, those 2 connection strings are needed because, Identity is gonna use the default one, and the .edmx is gonna use the other one. It is not possible to use the same one for both as the .edmx connection string needs metadata, that are not supported by identity. Thus, those 2 connection strings are different, but I modified them so they can refer to the same database.

基本上,这两个连接字符串是必需的,因为Identity将使用默认字符串,而.edmx将使用另一个。对于.edmx连接字符串需要元数据(标识不支持这些元数据),不可能同时使用相同的元数据。因此,这两个连接字符串是不同的,但是我对它们进行了修改,以便它们可以引用相同的数据库。

<connectionStrings> <add name="DefaultConnectionString" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=DatabaseName; Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="EntitiesConnectionString" connectionString="metadata=res://*/Models.WebAppModel.csdl|res://*/Models.WebAppModel.ssdl|res://*/Models.WebAppModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDb)\v11.0;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

< / connectionStrings >

The first connection string is the one you're gonna use with entity. So here is how the IdentityDbContext is initialized:

第一个连接字符串是你要使用的实体。这里是如何初始化IdentityDbContext的:

`` public class AppDbContext : IdentityDbContext { public AppDbContext() : base("QualityWebAppDbEntitiesDefault", throwIfV1Schema: false) { }

' ' ' public class AppDbContext: IdentityDbContext {public AppDbContext(): base(“QualityWebAppDbEntitiesDefault”,throwIfV1Schema: false)

    public static AppDbContext Create()
    {
        return new AppDbContext();
    }
}

`` (you can find the AppUser definition in the NakedIdentity sources)

’(你可以在NakedIdentity源中找到AppUser定义)

And for my .edmx, the initialization looks like this:

对于我的。edmx,初始化是这样的:

public partial class QualityWebAppDbEntities : DbContext { public QualityWebAppDbEntities() : base("name=QualityWebAppDbEntities") { } }

public partial类QualityWebAppDbEntities: DbContext {public QualityWebAppDbEntities(): base(“name=QualityWebAppDbEntities”)

Hope this will help people that want to use a Model First approach with Identity.

希望这将有助于那些想要使用模型优先的身份识别方法的人。

#1


3  

I said I'll post something in order to explain how I managed to get this working. I struggled to find some documentation and tutorials about using Identity with a Model First approach, so here it comes.

我说过我会发布一些东西来解释我是如何让它工作的。我很难找到一些关于使用模型第一方法的标识的文档和教程,所以它出现了。

Firstly, I found this genius article from Ben Foster: http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

首先,我从Ben Foster那里找到了这篇天才的文章:http://benfoster.io/blog/aspnet-id -stripped- baremail - part1。

This article discuss about how to implement an "Identity Stripped Bare". Reading it and following the steps allowed me understand how to use identity from scratch (you can see at the beggining that he generates an MVC 5 project with no authentication system). If you want the code of this "Naked Identity", here it is: https://github.com/benfoster/NakedIdentity

这篇文章讨论了如何实现一个“剥去的身份”。阅读它并遵循这些步骤让我了解如何从头开始使用标识(您可以看到,他生成了一个没有身份验证系统的MVC 5项目)。如果您想要这个“裸身份”的代码,这里是:https://github.com/benfoster/NakedIdentity

The first step was to generate the database. In the Naked Identity code, the v1.0 of Identity is used. Some things are different (a couple table properties for example), but it stays mostly identical. To create a database usable by Identity, I simply ran the template MVC 5 project, registered a user for the table to be created with the Code First approach, and then copied those tables needed by Identity in my empty database.

第一步是生成数据库。在Naked Identity代码中,使用了身份的v1.0。有些东西是不同的(例如一些表属性),但是它们基本上是相同的。要创建一个通过标识可用的数据库,我只需运行模板MVC 5项目,为使用代码优先方法创建的表注册一个用户,然后在我的空数据库中复制标识所需的表。

Once this was done, I generated my .edmx using the freshly created database. A connection string is added to the web.config. A connection string may already exist as "DefaultConnection". You need to keep this one as well.

完成此操作后,我使用新创建的数据库生成了.edmx。一个连接字符串被添加到web.config中。连接字符串可能已经作为“DefaultConnection”存在。你也需要保留这个。

Basically, those 2 connection strings are needed because, Identity is gonna use the default one, and the .edmx is gonna use the other one. It is not possible to use the same one for both as the .edmx connection string needs metadata, that are not supported by identity. Thus, those 2 connection strings are different, but I modified them so they can refer to the same database.

基本上,这两个连接字符串是必需的,因为Identity将使用默认字符串,而.edmx将使用另一个。对于.edmx连接字符串需要元数据(标识不支持这些元数据),不可能同时使用相同的元数据。因此,这两个连接字符串是不同的,但是我对它们进行了修改,以便它们可以引用相同的数据库。

<connectionStrings> <add name="DefaultConnectionString" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=DatabaseName; Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="EntitiesConnectionString" connectionString="metadata=res://*/Models.WebAppModel.csdl|res://*/Models.WebAppModel.ssdl|res://*/Models.WebAppModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDb)\v11.0;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

< / connectionStrings >

The first connection string is the one you're gonna use with entity. So here is how the IdentityDbContext is initialized:

第一个连接字符串是你要使用的实体。这里是如何初始化IdentityDbContext的:

`` public class AppDbContext : IdentityDbContext { public AppDbContext() : base("QualityWebAppDbEntitiesDefault", throwIfV1Schema: false) { }

' ' ' public class AppDbContext: IdentityDbContext {public AppDbContext(): base(“QualityWebAppDbEntitiesDefault”,throwIfV1Schema: false)

    public static AppDbContext Create()
    {
        return new AppDbContext();
    }
}

`` (you can find the AppUser definition in the NakedIdentity sources)

’(你可以在NakedIdentity源中找到AppUser定义)

And for my .edmx, the initialization looks like this:

对于我的。edmx,初始化是这样的:

public partial class QualityWebAppDbEntities : DbContext { public QualityWebAppDbEntities() : base("name=QualityWebAppDbEntities") { } }

public partial类QualityWebAppDbEntities: DbContext {public QualityWebAppDbEntities(): base(“name=QualityWebAppDbEntities”)

Hope this will help people that want to use a Model First approach with Identity.

希望这将有助于那些想要使用模型优先的身份识别方法的人。