How do I create connection string programmatically to MS SQL in Entity Framework 6?(如何在 Entity Framework 6 中以编程方式创建与 MS SQL 的连接字符串?)
问题描述
如何在 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 的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Entity Framework 6 中以编程方式创建与 MS SQL 的连接字符串?


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01