vico
vico

Reputation: 18171

Default SQL Server for Entity Framework

I have two instances of SQL Server - SQLEXPRESS and MSSQLServer 2017. My Entity Framework test application creates database in SQLEXPRESS. Where is it defined which instance of SQL Server Entity Framework should use?

Test application:

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class PeopleContext : DbContext
    {
        public IDbSet<Person> People { get; set; }
    }

    static void Main(string[] args) // using DB
    {
        try
        {
            using (PeopleContext ctx = new PeopleContext())
            {
                ctx.People.Add(new Person() { Id = 1, Name = "John Doe" });
                ctx.SaveChanges();
            }

            using (PeopleContext ctx = new PeopleContext())
            {
                Person person = ctx.People.SingleOrDefault(p => p.Id == 1);
                Console.WriteLine(person.Name);
            }

        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message);
        }

        Console.WriteLine("finish");
        Console.ReadLine();
    }

UPD: I have several connection strings in my app.config:

<?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>
  <connectionStrings>
    <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="LearnCSharpConn_server" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=LearnCSharp;Integrated Security=True" />
    <add name="LearnCSharpConn" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\LearnCSharp.mdf;Integrated Security=True" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

How EF decides which one to use?

Upvotes: 0

Views: 810

Answers (2)

MRsa
MRsa

Reputation: 684

To discover the server, username, password and default database you should seek to find the connection string being used by Entity Framework.

You do not specify which version of Entity framework you use, however most versions store the connection string in either web.config or app.config.

Alternatively, you may step into the PeopleContext constructor via Visual Studio's debug mode to view a connection string, or in .Net Core the connection string can be found by stepping through the OnConfigure method in PeopleContext.cs.

Failing that you may Ctrl + f to search connectionString over entire solution.

Upvotes: 0

JPocoata
JPocoata

Reputation: 732

You could use this:

public class PeopleContext : DbContext
{
    public PeopleContext() : base("Database") //it makes reference to the connection string defined in the app.config
    {     
    }
    public IDbSet<Person> People { get; set; }
}

And in your app.config:

<connectionStrings>
<add name="Database" connectionString="Data Source=source;Initial Catalog=Your_database;Integrated Security=True" providerName="System.Data.SqlClient" />

Upvotes: 1

Related Questions