1

In Visual Studio Community 2015 RC, I opened an ASP.NET MVC project for "individual user accounts," which uses the Microsoft.AspNet.Identity framework to create and manage users, logins, roles and permissions. It uses ASP.NET MVC 5.2 and Entity Framework version 6.

It appears that the only way to add something to this Microsoft created database is using the code first approach by first enabling migrations in the project, adding members to the DbContext inherited class and then running the update-database command at the PowerShell / Nuget package manager console. That does update the SQL Server database with the changes I made in my C# code.

However, is there a way to do the opposite? That is, to have changes that you made to the SQL Server database also reflect in your DbContext and code everywhere else? It appears that there isnt. This is a one-way street. I tried making some changes, and running the update-database command again but nothing happened.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • You would need to use the database-first approach with an EDMX-based context to get db changes to sync down to the code model(s). https://www.asp.net/mvc/overview/getting-started/database-first-development/setting-up-database – danludwig Aug 03 '15 at 12:55
  • That's what I fear. I know how to do that. But I am asking how to mix both. When you get an asp.net generated db, you're pretty much stuck to doing code first. How do you now hop off this bridge and get on the new one without destroying everything of the old one? – Water Cooler v2 Aug 03 '15 at 12:58
  • 1
    Does this help? http://stackoverflow.com/questions/19940014/asp-net-identity-with-ef-database-first-mvc5 or any of the other results here: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=asp.net%20identity%20database%20first – danludwig Aug 03 '15 at 13:02
  • Thank you. But holy cow! That's a pain. But thank you. That was exactly what my question was. – Water Cooler v2 Aug 03 '15 at 13:06
  • Really, the only thing you gain with db-first is the ability to auto-generate the code models after a manual change to the db schema. You can still update your code models manually to reflect changes to the db schema, and it's not that difficult. What is the major reason you prefer db- over code-first? – danludwig Aug 03 '15 at 13:11
  • Two challenges: (1) habit, which I can work around. Other than adding a DbSet or some sort of queryable and a POCO for every table I add, is there any other place I need to change in the code? I think not but am not sure. What if I add stored procs? User defined functions? How do I manually reflect those in the code? (2) I'm using Microsoft.AspNet.Identity which generates a code-first db, which you have to carry along with. If I don't, I have to re-engineer a wheel that it already provides. What I gain by composition, I lose by having myself tied to something I am not familiar with. – Water Cooler v2 Aug 03 '15 at 13:24
  • Also, I will have business data as well, which I want to keep ideally in the same database but now will have to keep separate because of this whole paradigm of code first. Now. I have to introduce more complexity by having one authentication/membership database that asp.net generates for me for which I have to do code first, and then one EDM based business database. And querying data by joining both will also be somewhat complicated, I imagine. – Water Cooler v2 Aug 03 '15 at 13:26
  • If I don't go with the asp.net generated code first one, I will have to re-write everything they provide (OAuth, forms auth, cookie management, token cancellation and expiration, etc.) from scratch, which means lots of time and testing and money. – Water Cooler v2 Aug 03 '15 at 13:27

0 Answers0