问题
I would like for it to use SQL Server by default for everything when I start a new ASP.NET MVC 4 Web Application project. By default when you run this project it creates a LocalDb instance and creates the following tables in it:
- webpages_Membership
- webpages_OAuthMembership
- webpages_Roles
- webpages_UsersInRoles
- UserProfile
I have been able to use a code first migration to put the UserProfile table into a SQL server database. However, I have not been able to accomplish this with the other 4 tables.
Obviously my goal is to start with a single SQL Server database and have all of the tables for the application contained within the single database.
回答1:
Open your InitializeSimpleMembershipAttribute.cs file, this is where the WebSecurity database initialization is. You need to modify it with the correct connectionStringName. Example:
Change
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
to
WebSecurity.InitializeDatabaseConnection("MyRealDBConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
IF you want to copy the structure of the web security tables to use with Code First, there is an article .... which I cannot find at the moment ... give me a few.
Well I could not find it - but honestly - it was a pain. The easiest way, since you have the DB generated already, might be to use an Code First reverse engineer tool like Entity Framework Power Tools. This will do most of the work for you. Then just add the classes to your DbContext, create a migration, and update your real database.
Also - you may need to make more modifications than this - depending on your Context name and such.
回答2:
If your goal is to only create those tables in your existing db, you should perform 2 steps.
First, change the connection string of db initialization as what @Jack said.
Second, in your DbContext, change the constructor like this:
public class MyDbContext : DbContext
{
publicMytDbContext()
: base("MyRealDBConnection")
{
}
// ...
}
来源:https://stackoverflow.com/questions/17460372/is-there-a-way-to-make-ef-5-code-first-migrations-use-a-sql-server-database-in-a