问题描述
如何在 Entity Framework 6 中以编程方式创建与 MS SQL 的连接字符串?
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.
所以这是一个有效的 EF6 示例
So here is an EF6 example that works
App.Config
entityFramework codeConfigurationType="WPFwithEF.SqlConfiguration, WPFwithEF">/实体框架
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; }
}
配置.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
,如下所述:如何:构建一个EntityConnection连接字符串
You can use the EntityConnectionStringBuilder
as descriped here: How to: Build an EntityConnection Connection String
这篇关于如何在 Entity Framework 6 中以编程方式创建与 MS SQL 的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!