Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It seems I may have finally got this, and it may have just been one giant misunderstanding.</p> <p>As it turns out, I was expecting <code>((IObjectContextAdapter)context).ObjectContext.CreateDatabase();</code> to do what it simply doesn't, which is create all of the tables in the database that don't exist, or simply update them if they do and they're different.</p> <p>What actually happens is that it literally runs a <code>CREATE DATABASE</code> statement, which to me is the most useless thing ever. Unless you're working in a really strange environment, you will always have a database on the off and so it would always exist (and subsequently the table creation would never happen!), I'd rather not be giving real-world users access to create a database anyway.</p> <p>Anyway, I solved my specific issue of wanting <code>UserProfile</code> (and related tables) to create the database by using the <code>DropCreateDatabaseIfModelChanges</code> initializer, and forcing an initialization like below:</p> <pre><code>public SimpleMembershipInitializer() { #if DEBUG Database.SetInitializer&lt;DataContext&gt;(new DropCreateDatabaseIfModelChanges&lt;DataContext&gt;()); #else Database.SetInitializer&lt;DataContext&gt;(null); #endif try { using (var context = new DataContext()) { if (!context.Database.Exists()) { ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } context.Database.Initialize(true); } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "EmailAddress", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } } </code></pre> <p>.. this works and is perfect for development but is pretty useless in practice since it will literally drop the database and recreate it from scratch if the model changes. To me, this makes the whole code-first practice almost useless in it's default form and I'll probably end up reverting back to a from-DB edmx generation.</p> <p>The "mystery" behind the <code>UserProfile</code> table still being created is that <code>WebSecurity.InitializeDatabaseConnection</code> will initialise the table if it doesn't exist based on the fields you pass into it, which is why the <code>EmailAddress</code> was created instead of <code>UserName</code>, because I had changed it in this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload