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

c# entity-framework-6 sql-server wpf

Question

In Entity Framework 6, how do I programmatically build a connection string to MS SQL?

I was hoping someone might provide some guidance or point me in the direction of a resource that demonstrates how to set up connection strings programmatically in EF 6. I'm using C# and WPF. Although the MSDN article states that you can use http://msdn.microsoft.com/en-us/data/jj680699#moving, it doesn't cover how to create connection strings specifically.

So, here is a functional EF6 example.

App.Config

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

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());
    }
}
}

However, this configuration fails if the context base is "name=Wpf." Is there a method to make it work? And I'm seeking for the newest EF6 rather than the conventional approach.

1
11
2/12/2014 4:17:45 PM

Popular Answer

The can be used toEntityConnectionStringBuilder the following: How to create a connection string for an entity connection

3
2/12/2014 2:01:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow