如何ASP。NET Identity要与Model First方法一起使用吗?(复制)

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

This question already has an answer here:

这个问题已经有了答案:

I'm developing an ASP.NET MVC 5 application which rely on a Model First approach using Entity Framework 6. I've already generated my database using my .edmx and everything works fine.

我开发一个ASP。NET MVC 5应用程序,它依赖于使用实体框架6的模型。我已经使用.edmx生成了我的数据库,一切都运行良好。

Now, I'd like to add ASP.NET Identity to my database, but I don't know which approach should I use. Shall I add ASP.NET Identity using Code First with Migration?

现在,我想添加ASP。NET标识到我的数据库,但我不知道应该使用哪种方法。我要添加ASP。NET身份首先使用代码进行迁移?

I would like to maintain my .edmx so that I can update my database afterwards, as well as having migrations enabled so that I can modify my ASP.NET Identity entities thereafter too.

我想维护我的。edmx,这样我就可以在以后更新我的数据库,同时允许迁移,这样我就可以修改我的ASP。NET Identity entity then too。

2 个解决方案

#1


1  

Nowadays the recommended approach is to use a separate database for authentication. This approach is enforced in ASP.NET Identity. While you could add the ASP.NET Identity tables to your application database somehow (Migrations, or manually), I would discourage you from doing so as you would only make your life harder as you would no longer be in the intended usage of the framework.

现在推荐的方法是使用单独的数据库进行身份验证。这种方法在ASP中得到了实施。净的身份。而你可以添加ASP。在您的应用程序数据库中(迁移,或手动),我将阻止您这样做,因为您只会使您的生活变得更加困难,因为您将不再使用框架的预期用途。

The way to go is to keep the scaffolded ASP.NET Identity DbContext and match the ASP.NET Identity Users to your application Users entities using some field, for example the email address field. The underlying idea is that you would use this same identity database to authenticate users against OAuth (Microsoft Live, Google, Facebook, etc...) thus providing you with a unified way to do authentication.

要走的路就是要保持脚手架的结构。NET Identity DbContext并匹配ASP。使用某些字段(例如电子邮件地址字段)为应用程序用户实体的NET标识用户。其基本思想是使用相同的身份数据库对OAuth (Microsoft Live、谷歌、Facebook等)用户进行身份验证,从而为您提供一种统一的身份验证方式。

#2


0  

With some experience with asp.net mvc identity I have to say that it is naturally meant (by conventions) for code first approach. you have to apply some of your hacks to use it with database first. if you search on the web you would find all sort of user derived techniques of using it with database first

有了asp.net mvc的一些经验,我不得不说对于代码优先的方法来说,它自然意味着(按照惯例)。您必须首先应用您的一些技巧来使用数据库。如果你在网上搜索,你会发现所有的用户派生的技术,首先使用它与数据库

I have used asp.net identity with database first approach using EF with two connection strings and single database. I manually transferred all the users tables which i created by registering the users to my main database which contains my others tables as well

我使用asp.net identity和database first方法,使用EF和两个连接字符串和一个数据库。我通过注册用户将创建的所有用户表手动转移到包含其他表的主数据库中

<connectionStrings>

  <add name="DefaultConnection" connectionString="data source=localhost;database=LocalLoanDb;user id=sa;password=123456;" providerName="System.Data.SqlClient" />

  <add name="LocalLoanDbEntities" connectionString="metadata=res://*/Models.LoanDataModel.csdl|res://*/Models.LoanDataModel.ssdl|res://*/Models.LoanDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=LocalLoanDb;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

so in this way defaultconnection was meant for identity tables and i use other connection string to connect with my project's repository. so without any architectural change in the identity framework i managed to use it very happily in my application.

因此,在这种情况下,defaultconnection是用于身份表的,我使用其他连接字符串来连接我的项目的存储库。因此,在身份框架中没有任何架构更改的情况下,我在应用程序中非常愉快地使用了它。

#1


1  

Nowadays the recommended approach is to use a separate database for authentication. This approach is enforced in ASP.NET Identity. While you could add the ASP.NET Identity tables to your application database somehow (Migrations, or manually), I would discourage you from doing so as you would only make your life harder as you would no longer be in the intended usage of the framework.

现在推荐的方法是使用单独的数据库进行身份验证。这种方法在ASP中得到了实施。净的身份。而你可以添加ASP。在您的应用程序数据库中(迁移,或手动),我将阻止您这样做,因为您只会使您的生活变得更加困难,因为您将不再使用框架的预期用途。

The way to go is to keep the scaffolded ASP.NET Identity DbContext and match the ASP.NET Identity Users to your application Users entities using some field, for example the email address field. The underlying idea is that you would use this same identity database to authenticate users against OAuth (Microsoft Live, Google, Facebook, etc...) thus providing you with a unified way to do authentication.

要走的路就是要保持脚手架的结构。NET Identity DbContext并匹配ASP。使用某些字段(例如电子邮件地址字段)为应用程序用户实体的NET标识用户。其基本思想是使用相同的身份数据库对OAuth (Microsoft Live、谷歌、Facebook等)用户进行身份验证,从而为您提供一种统一的身份验证方式。

#2


0  

With some experience with asp.net mvc identity I have to say that it is naturally meant (by conventions) for code first approach. you have to apply some of your hacks to use it with database first. if you search on the web you would find all sort of user derived techniques of using it with database first

有了asp.net mvc的一些经验,我不得不说对于代码优先的方法来说,它自然意味着(按照惯例)。您必须首先应用您的一些技巧来使用数据库。如果你在网上搜索,你会发现所有的用户派生的技术,首先使用它与数据库

I have used asp.net identity with database first approach using EF with two connection strings and single database. I manually transferred all the users tables which i created by registering the users to my main database which contains my others tables as well

我使用asp.net identity和database first方法,使用EF和两个连接字符串和一个数据库。我通过注册用户将创建的所有用户表手动转移到包含其他表的主数据库中

<connectionStrings>

  <add name="DefaultConnection" connectionString="data source=localhost;database=LocalLoanDb;user id=sa;password=123456;" providerName="System.Data.SqlClient" />

  <add name="LocalLoanDbEntities" connectionString="metadata=res://*/Models.LoanDataModel.csdl|res://*/Models.LoanDataModel.ssdl|res://*/Models.LoanDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=LocalLoanDb;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

so in this way defaultconnection was meant for identity tables and i use other connection string to connect with my project's repository. so without any architectural change in the identity framework i managed to use it very happily in my application.

因此,在这种情况下,defaultconnection是用于身份表的,我使用其他连接字符串来连接我的项目的存储库。因此,在身份框架中没有任何架构更改的情况下,我在应用程序中非常愉快地使用了它。