Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COYou do realize that this is what EF Migrations is for, correct? It's useless because that's not the way you migrate from one version to another. EF has a specific use case for this. Also, your question said you deleted the database each time, when it appears you were not actually deleting it, but expecting EF to magically change the schema for you. Had you actually given us the correct information, we could have given you a real response. That's why you got a lot of head scratching, because your question said you were doing things that you didn't do.
      singulars
    2. COI'm not sure why you would think that something called `CreateDatabase()` didn't actually literally create a database. It's not useless, because it's designed to create the database when it doesn't exist, which is a situation you told us was the case.
      singulars
    3. CO@MystereMan Yes I'm aware of this and that is exactly what my post summarized this as; a misunderstanding! It partially stemmed from other SO posts related to similar issues where it was told that 'changing the class automatically updated the tables'. The information I gave in so far as 'deleting the database' was of course incorrect as I was actually deleting the database *contents*. My confusion came from the fact that my `UserProfile` table was still being created (albeit incorrectly), but now I realise it's the `InitializeDatabaseConnection`. It's been a **very** long few weeks :)
      singulars
 

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