How to generate DB on remote SQL Server using EF Code first? RRS feed

  • Question

  • There are examples of connection strings in documentation to CF, but they are strange and looks non-standard. For example, instead of "Data Source" parameter, there is a "Server" parameter. Also no examples of how to set connection string to SQL server with SQL Server authentication. I have no SQL server on my machine and I don't want to install it.
    I wrote a connection string, but it doesn't work:
        name="OrdersContext" providerName="System.Data.SqlClient"
        connectionString="Data Source=xx.xx.xx.xx;Initial Catalog=Realcom;Persist Security Info=True;User ID=sa;Password=xxxxxx; Trusted_Connection=true;Integrated Security=True;MultipleActiveResultSets=True"/>

    Nothing happens.
    No errors, warnings, etc.
    I have DBContextInitializer, I'm creating instance of it, but nothing happens! Help!
    Thursday, September 29, 2011 1:21 PM


  • Hello.

    You write a connection string for Code First the same way as you do for ADO.NET.

    (here the meaning of "Server" the same as "Data Source" attribute;  you can find some information on that matter here http://www.techrepublic.com/article/educate-yourself-about-net-sql-server-connection-string-syntax/6084879 )

    But you should know when database initialization code is run:

    - when you call Database.SetInitializer method - it performs only registration of a IDatabaseInitializer<TContext> object with your Context; but doesn't execute  any code from it;

    - EF creates your EDM only when you construct your Context object the first time;

    - all modification to your database (creating database, running the code in your DBContextInitializer class to Seed data) happens only when you request data from your context (it means from your database as well) the first time.

    // no model EDM exists here yet
    // no database initialization code is run yet
    using(var context = new MyContext())
      // EDM is constructed
      // no database initialization code is run yet
      var query = context.Companies.FirstOrDefault();
      // database initialization code has been run to this point

    Hope this helps.


    • Proposed as answer by Kris444 Friday, September 30, 2011 1:24 AM
    • Marked as answer by Larcolais Gong Thursday, October 6, 2011 3:54 AM
    Thursday, September 29, 2011 4:21 PM