2

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.

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?

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.

2 Answers2

0

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.

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.

tobiak777
  • 3,175
  • 1
  • 32
  • 44
  • That would be neater indeed, but I've an Azure subscription and each SQL Server database costs me some money. Taking this into account, do you think it still the best option for me? –  Jan 09 '16 at 18:40
  • @bbougot I agree I faced the same situation, Microsoft is smart... ; ) Only you can know what's best for your circumstance I'm afraid, if you can I'd definitely advise to go with a 2nd DB on the least expensive tiers, but if really you can't spend, you might want to put everything in the same DB. It won't kill you overnight – tobiak777 Jan 09 '16 at 18:44
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

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

<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.

maztt
  • 12,278
  • 21
  • 78
  • 153
  • 1
    Identity at its core isn't tied to ef so there is no "natural" approach. The default approach you refer to are the entity framework namespaces. – jamesSampica Jan 09 '16 at 20:21