none
CTP5: Using Sql server with Code First and database creation instead of Sql Express

    Question

  • Hi all,

    I've started using CTP5 and didn't have SQL Express installed. So I figured that I would work with SQL Server Standard. I added the connection string to web.config pointing to an empty SQL Server Standard database, run the app, when when I try to use the datacontext I get:

    "Model compatibility cannot be checked because the database does not contain model metadata."

    I've tried adding a DropCreateDatabaseIfModelChanges to global.asax but still nothing.

    My connection string is: "Data Source=.;Initial Catalog=TestCTP5;User ID=testctp5;Password=testctp5"

    The user testctp5 is db_owner.

    So my question is: Is it possible to use database creation with a standard sql server or only with express?


    bruno
    Saturday, December 11, 2010 9:09 PM

Answers

  • Bruno

    I think it's just that you are missing a reference to the provider in your connection.

     

    I've got this as my connection string in my config file:

     

        <add

         name="AWEntities" 
         connectionString="Data Source=.;Initial Catalog=AdventureWorksSuperLT_TWO;Integrated Security=True;MultipleActiveResultSets=True"
         providerName="System.Data.SqlClient"/>

     

    If the db doesn't exist in my sql server instance, it gets created on the fly. I have no other reference/pointer to the database.

    I have a blog post about working with the non-express db and CTP5 at http://thedatafarm.com/blog/data-access/looking-at-ef4-ctp5-in-parts-part-4-ndash-working-with-new-and-existing-databases-in-code-first/

    hth

    julie

    Saturday, December 11, 2010 9:19 PM

All replies

  • Bruno

    I think it's just that you are missing a reference to the provider in your connection.

     

    I've got this as my connection string in my config file:

     

        <add

         name="AWEntities" 
         connectionString="Data Source=.;Initial Catalog=AdventureWorksSuperLT_TWO;Integrated Security=True;MultipleActiveResultSets=True"
         providerName="System.Data.SqlClient"/>

     

    If the db doesn't exist in my sql server instance, it gets created on the fly. I have no other reference/pointer to the database.

    I have a blog post about working with the non-express db and CTP5 at http://thedatafarm.com/blog/data-access/looking-at-ef4-ctp5-in-parts-part-4-ndash-working-with-new-and-existing-databases-in-code-first/

    hth

    julie

    Saturday, December 11, 2010 9:19 PM
  • Bruno -

    Your connection string should look like this:

    <add name="TestCTP5" connectionString="Data Source=(localhost);Initial Catalog=TestCTP5;Integrated Security=True" providerName="System.Data.SqlClient" />

    Your constructure for your data context should look like this:

     public class Database : DbContext {
      public Database() : base("TestCTP5") {
      }
    ...

    Just for now, create a class called DatabaseInitializer that looks like this:

    public class DatabaseInitializer : DropCreateDatabaseAlways<Database> {
      protected override void Seed(Database database) {
      }
    }

    Now in your global.asax.xx add this line to your app_start method:

    DbDatabase.SetInitializer(new DatabaseInitializer());

     

    When you run your app and try to use the context, it will create a database called TestCTP5


    Paul Barton http://posthope.com
    • Proposed as answer by Mastro Wednesday, March 02, 2011 2:20 PM
    Saturday, December 11, 2010 9:37 PM
  • Hi Julie,

    I had the provider on the connection string.

    I figured out what has my "mistake": Since I previously created the database the EF didn't used database creation.

    Thanks.

     

    For me this seems to make no sense, since the database was empty, EF could use it and do creation/update. What do you think?

     

    Once again, thanks.


    Bruno Figueiredo
    Saturday, December 11, 2010 9:43 PM
  • it's the 'evolution' part still missing
    empty Db is not what it wants, it wants and looks for EdmMetadata table I think
    and it doesn't know yet how to 'upgrade' your existing data, empty or not
    and this often happens even later on during the development - what I typically do is go and drop the Db manually, rebuild and restart app - that usually solves all similar issues, if you see no metadata or similar just drop/recreate
    to recap...
    - if you use credentials, they should be able to create Db as well, so you cannot just simply as normally create credentials for one database, as it's dropped recreated all the time
    - names of your context and connection should match (or explicitly specified thru the specific construction as mentioned)
    - and keep 'drop' initializer for start at least
    - sometimes restart things - e.g. if in ASP.NET it may cache for your 'app domain'
    Gaga
    Saturday, December 11, 2010 11:06 PM
  • and this often happens even later on during the development - what I typically do is go and drop the Db manually, rebuild and restart app -

    FYI: Calling delete can save one the manual step of deleting the database by hand.

    using (var db = new ReservesContext()) 
    {
       db.Database.Delete();
       ...
    }
    

    William Wegerson (www.OmegaCoder.Com)
    Friday, March 04, 2011 4:18 PM