none
Entity Framework doesn't create the database in SQL Server Management 2012, instead it just creates in (localdb)\v11.0 How to fix it? RRS feed

  • Question

  • Hi, I'm Begginer in Entity Framework. Then I'm having some problems.

    I created my entities and I give Save Changes. (Ok)

    Then, I Tried to Connect with the database through Server Explorer, if I use the instance of the SQL Server Management Studio 2012(PC-PAULO\INSTANCIAPJ) as Server Name, the database doesn't appears but, if I use the the instance (localdb)\v11.0 the dabase appears.

    How to change the save to the SQL Server Management Studio 2012, I don't know where this (localDB)\v11.0 saves.

    Help-me Thank you :D

    Sunday, January 4, 2015 9:11 PM

Answers

  • Hello PauloJos,

    The code should be ok and it is really strange that the database is only created into the local database in your machine. I am wondering if it is related with the machine environment, could you please have a try with below demo on your machine which works on my side and if it is possible, please have a try to run your application on other machines to see if it works:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                #region https://social.msdn.microsoft.com/Forums/en-US/00eff3c4-2336-4807-9212-e6f2a6c8812e/entity-framework-doesnt-create-the-database-in-sql-server-management-2012-instead-it-just-creates?forum=adodotnetentityframework
    
                using (CFDbContext db = new CFDbContext())
    
                {
    
                    db.Database.CreateIfNotExists();
    
                }
    
                #endregion
    
            }
    
        }
    
    
        public class CFDbContext : DbContext
    
        {
    
            public CFDbContext() : base("UltraFoda3") { }
    
            public DbSet<TestOne> TestOnes { get; set; }
    
        }
    
    
        public class TestOne
    
        {
    
            public int ID { get; set; }
    
            public string Name { get; set; }
    
        }

    Config file is similar:

    <?xml version="1.0" encoding="utf-8"?>
    
    <configuration>
    
           <configSections>
    
                  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    
                  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    
           </configSections>
    
           <entityFramework>
    
                  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    
                         <parameters>
    
                               <parameter value="v11.0" />
    
                         </parameters>
    
                  </defaultConnectionFactory>
    
                  <providers>
    
                         <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    
                  </providers>
    
           </entityFramework>
    
           <connectionStrings>
    
                  <add name="UltraFoda3" connectionString="Data Source=YourServer;Initial Catalog=UltraFoda3;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    
           </connectionStrings>
    
    </configuration>

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, January 7, 2015 6:49 AM
    Moderator

All replies

  • Hi, I'm Begginer in Entity Framework. Then I'm having some problems.

    I created my entities and I give Save Changes. (Ok)

    Then, I Tried to Connect with the database through Server Explorer, if I use the instance of the SQL Server Management Studio 2012(PC-PAULO\INSTANCIAPJ) as Server Name, the database doesn't appears but, if I use the the instance (localdb)\v11.0 the dabase appears.

    How to change the save to the SQL Server Management Studio 2012, I don't know where this (localDB)\v11.0 saves.

    Help-me Thank you :D

    I use the Visual Studio 2013.
    Sunday, January 4, 2015 9:12 PM
  • Hello PauloJos,

    According to your description, you are using Entity Framework Code First approach and for why it just creates in (localdb)\v11.0, this is by designed if you do not specify a database instance. The (localdb)\v11.0 is an inside instance installed with Visual Studio together which is usually used to store Code First database.

    If you want your database to be generated to a specific instance, you could add/change the constructor as below:

    public class CFContext : DbContext
    
        {
    
            public CFContext()
    
                : base("TestDB")
    
            {
    
            }
    
            public DbSet<Geometry> Geometries { get; set; }
    
    } 
    

    In your app/web.config file, add a connect string in the conectionstring section and change the database instance name and database name:

    <connectionStrings>
    
        <add name="TestDB" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=TestDB;Integrated Security=True;MultipleActiveResultSets=True"
    
          providerName="System.Data.SqlClient" />
    
      </connectionStrings>
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by JayChase Monday, January 5, 2015 2:49 AM
    Monday, January 5, 2015 2:07 AM
    Moderator
  • Hi Jay Chase. How Are you?

    I'm using an Console Application and it has not an Web.config but App.Config.

    The file app.config doesn't contain the section "ConnectionString", Then I created.

    Then, I made that you said but it doesn't work.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="UltraFoda3" connectionString="Data Source=PC-PAULO\INSTANCIAPJ;Initial Catalog=UltraFoda3;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
      </connectionStrings>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

    Then, it continues saving in (dblocal)\v11.0 :(

    What's will be the problem?

    Thanks :D





    Monday, January 5, 2015 4:38 AM
  • Hello,

    >> Then, I made that you said but it doesn't work.

    Could you please share your DbContext class? I just want to make sure that you have the constructor inherited the based on which is important. In your case, it should look like as below:

    public class CFContext : DbContext
    
        {
    
            public CFContext()
    
                : base("UltraFoda3")
    
            {
    
            }
    
            public DbSet<Geometry> Geometries { get; set; }
    
    }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 5, 2015 6:22 AM
    Moderator
  • public class MvpShowCastDbContext :DbContext
        {
            public MvpShowCastDbContext() : base("UltraFoda3") { }
    
            public DbSet<Trilha> Trilhas { get; set; }
            public DbSet<Palestrante> Palestrantes { get; set; }
            public DbSet<Palestra> Palestras { get; set; }
        }

    It's it. 

    Thanks :D 

    Tuesday, January 6, 2015 7:45 PM
  • Hello PauloJos,

    The code should be ok and it is really strange that the database is only created into the local database in your machine. I am wondering if it is related with the machine environment, could you please have a try with below demo on your machine which works on my side and if it is possible, please have a try to run your application on other machines to see if it works:

    class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                #region https://social.msdn.microsoft.com/Forums/en-US/00eff3c4-2336-4807-9212-e6f2a6c8812e/entity-framework-doesnt-create-the-database-in-sql-server-management-2012-instead-it-just-creates?forum=adodotnetentityframework
    
                using (CFDbContext db = new CFDbContext())
    
                {
    
                    db.Database.CreateIfNotExists();
    
                }
    
                #endregion
    
            }
    
        }
    
    
        public class CFDbContext : DbContext
    
        {
    
            public CFDbContext() : base("UltraFoda3") { }
    
            public DbSet<TestOne> TestOnes { get; set; }
    
        }
    
    
        public class TestOne
    
        {
    
            public int ID { get; set; }
    
            public string Name { get; set; }
    
        }

    Config file is similar:

    <?xml version="1.0" encoding="utf-8"?>
    
    <configuration>
    
           <configSections>
    
                  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    
                  <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    
           </configSections>
    
           <entityFramework>
    
                  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    
                         <parameters>
    
                               <parameter value="v11.0" />
    
                         </parameters>
    
                  </defaultConnectionFactory>
    
                  <providers>
    
                         <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    
                  </providers>
    
           </entityFramework>
    
           <connectionStrings>
    
                  <add name="UltraFoda3" connectionString="Data Source=YourServer;Initial Catalog=UltraFoda3;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    
           </connectionStrings>
    
    </configuration>

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, January 7, 2015 6:49 AM
    Moderator
  • Hello PauloJoséBR,

    Any update? I have marked my reply as answer since I think it is helpful, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 16, 2015 9:37 AM
    Moderator