我如何编程创建连接字符串MS SQL实体框架6? [英] How do I create connection string programmatically to MS SQL in Entity Framework 6?

查看:167
本文介绍了我如何编程创建连接字符串MS SQL实体框架6?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何编程创建连接字符串MS SQL实体框架6?

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

我使用C#和WPF,我想知道,如果有人可以告诉我怎么或链接我演示了如何编程设置连接字符串中EF资源6. MSDN文章解释,你可以的 http://msdn.microsoft.com/en-us/data/jj680699#moving 但它并没有进入实际创建连接字符串。

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.

所以这里的作品

App.Config中

App.Config

的EntityFramework codeConfigurationType =WPFwithEF.SqlConfiguration的EF6例子,WPFwithEF>
/的EntityFramework

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

上下文

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



Configuration.cs

Configuration.cs

namespace WPFwithEF
{
public class SqlConfiguration : DbConfiguration
{

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



但如果上下文基地NAME = WPF那么这个设置不工作是有办法,使这项工作?我期待最新EF6不老的方式来做到这一点。

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.

推荐答案

您可以使用 EntityConnectionStringBuilder 这里descriped:的如何构建一个EntityConnection连接字符串

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

这篇关于我如何编程创建连接字符串MS SQL实体框架6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆