Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create connection string programmatically to MS SQL in Entity Framework 6?

How do I create connection string programmatically to MS SQL in Entity Framework 6?

I'm using c# and WPF and I was wondering if someone could show me how or link me to a resource that shows how to set up connection strings programmatically in EF 6. The MSDN article explains that you can http://msdn.microsoft.com/en-us/data/jj680699#moving but it doesn't go into creating actual connection strings.

So here is an EF6 example that works

App.Config

entityFramework codeConfigurationType="WPFwithEF.SqlConfiguration, WPFwithEF"> /entityFramework

context

public class ProductContext : DbContext
{  
    public ProductContext():base("Wpf")
    { }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

Configuration.cs

namespace WPFwithEF
{
public class SqlConfiguration : DbConfiguration
{

    public SqlConfiguration()
    {
        SetProviderServices(SqlProviderServices.ProviderInvariantName,SqlProviderServices.Instance);           
        SetDefaultConnectionFactory(new SqlConnectionFactory());
    }
}
}

but if the context base is "name=Wpf" then this set up does not work is there a way to make that work? And i'm looking for the latest EF6 not the old way to do it.

like image 426
Bob Avatar asked Feb 12 '14 13:02

Bob


People also ask

How do I create a SQL connection string?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.


2 Answers

You can use the EntityConnectionStringBuilder as descriped here: How to: Build an EntityConnection Connection String

like image 94
csteinmueller Avatar answered Oct 11 '22 16:10

csteinmueller


If you are specifically connecting to a MS Sql database, this should work:

private DbConnection CreateConnection(string connectionString)
{
    return new SqlConnection(connectionString);
}

private string CreateConnectionString(string server, string databaseName, string userName, string password)
{
    var builder = new SqlConnectionStringBuilder
    {
        DataSource = server, // server address
        InitialCatalog = databaseName, // database name
        IntegratedSecurity = false, // server auth(false)/win auth(true)
        MultipleActiveResultSets = false, // activate/deactivate MARS
        PersistSecurityInfo = true, // hide login credentials
        UserID = userName, // user name
        Password = password // password
    };
    return builder.ConnectionString;
}

how to use:

public void ConnectoToDbWithEf6()
{
    using(var connection = CreateConnection(CreateConnectionString("server", "db", "you", "password")
    {
        using(var context = new YourContext(connection, true))
        {
            foreach(var someEntity in context.SomeEntitySet)
            {
                Console.WriteLine(someEntity.ToString());
            }
        }
    }

}

see https://msdn.microsoft.com/en-Us/library/system.data.sqlclient.sqlconnectionstringbuilder%28v=vs.100%29.aspx

like image 43
hoekki Avatar answered Oct 11 '22 15:10

hoekki