如何在 entity framework 数据库第一个模型连接字符串中动态包含工作站 ID?
How to include work station id in entity framework database first model connection string dynamically?
我有一个项目在它的数据中使用 entity framework 数据模型(.edmx) layer.I 想在连接字符串中添加工作站 ID在数据库中创建日志时存储它。
我就是这样做的:
var d = new PresentModelConnectionString();
string connectionString = d.Database.Connection.ConnectionString;
string lastCharacter = connectionString.Substring(connectionString.Length - 1, 1);
if (lastCharacter == ";")
{
connectionString += $"workstation id={Helpers.UserId.ToString()}";
}
else
{
connectionString += $";workstation id={Helpers.UserId.ToString()}";
}
d.Database.Connection.ConnectionString = connectionString;
return d;
但是当它尝试连接到数据库并获取数据时 returns 用户 sa 的登录失败。当我删除此行时:
d.Database.Connection.ConnectionString = connectionString;
它工作正常。
这是连接字符串:
<add name="PresentModelConnectionString" connectionString="metadata=res://*/PresentModel.csdl|res://*/PresentModel.ssdl|res://*/PresentModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.1.101\sql2014;initial catalog=MIS;user id=sa;password=sa_123;connect timeout=600000000;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
如何将工作站 ID 动态包含到连接字符串中?
谢谢
搜索几个小时后我找到了这个解决方案,它首先更改 entity framework 数据库的连接字符串 model.As Alex 评论中推荐我应该使用 SqlConnectionStringBuilder 制作连接字符串。
在单独的文件中为模型上下文创建部分 class,以添加将连接字符串作为参数的构造函数:
public partial class PresentModelConnectionString
{
public PresentModelConnectionString(string connectionString):base(connectionString)
{
}
}
制作连接字符串:
//connection string in web.config
//Data Source=192.168.1.101\sql2014;Initial Catalog=MIS_Keshavarzi_980906;user id=sa;pwd=sa_123; Connect Timeout=60000;
string connectionString =
System.Configuration.ConfigurationManager.AppSettings["ABSConnectionString"];
SqlConnectionStringBuilder connectionStringBuilder = new
SqlConnectionStringBuilder(connectionString);
connectionStringBuilder.WorkstationID = Helpers.UserId.ToString(); //get work station id
connectionStringBuilder.ApplicationName = "EntityFramework"; //set application name
要修改模型连接字符串,请使用 EntityConnectionStringBuilder:
EntityConnectionStringBuilder entityConnectionBuilder = new
EntityConnectionStringBuilder();
entityConnectionBuilder.Metadata =
"res://*/PresentModel.csdl|res://*/PresentModel.ssdl|res://*/PresentModel.msl";
entityConnectionBuilder.Provider = "System.Data.SqlClient";
entityConnectionBuilder.ProviderConnectionString =
connectionStringBuilder.ConnectionString;
PresentModel 应随您的型号名称而变化。
最后使用此连接字符串创建模型上下文的新实例:
var entityContext = new
PresentModelConnectionString(entityConnectionBuilder.ConnectionString);
我有一个项目在它的数据中使用 entity framework 数据模型(.edmx) layer.I 想在连接字符串中添加工作站 ID在数据库中创建日志时存储它。
我就是这样做的:
var d = new PresentModelConnectionString();
string connectionString = d.Database.Connection.ConnectionString;
string lastCharacter = connectionString.Substring(connectionString.Length - 1, 1);
if (lastCharacter == ";")
{
connectionString += $"workstation id={Helpers.UserId.ToString()}";
}
else
{
connectionString += $";workstation id={Helpers.UserId.ToString()}";
}
d.Database.Connection.ConnectionString = connectionString;
return d;
但是当它尝试连接到数据库并获取数据时 returns 用户 sa 的登录失败。当我删除此行时:
d.Database.Connection.ConnectionString = connectionString;
它工作正常。
这是连接字符串:
<add name="PresentModelConnectionString" connectionString="metadata=res://*/PresentModel.csdl|res://*/PresentModel.ssdl|res://*/PresentModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.1.101\sql2014;initial catalog=MIS;user id=sa;password=sa_123;connect timeout=600000000;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
如何将工作站 ID 动态包含到连接字符串中?
谢谢
搜索几个小时后我找到了这个解决方案,它首先更改 entity framework 数据库的连接字符串 model.As Alex 评论中推荐我应该使用 SqlConnectionStringBuilder 制作连接字符串。
在单独的文件中为模型上下文创建部分 class,以添加将连接字符串作为参数的构造函数:
public partial class PresentModelConnectionString
{
public PresentModelConnectionString(string connectionString):base(connectionString)
{
}
}
制作连接字符串:
//connection string in web.config
//Data Source=192.168.1.101\sql2014;Initial Catalog=MIS_Keshavarzi_980906;user id=sa;pwd=sa_123; Connect Timeout=60000;
string connectionString =
System.Configuration.ConfigurationManager.AppSettings["ABSConnectionString"];
SqlConnectionStringBuilder connectionStringBuilder = new
SqlConnectionStringBuilder(connectionString);
connectionStringBuilder.WorkstationID = Helpers.UserId.ToString(); //get work station id
connectionStringBuilder.ApplicationName = "EntityFramework"; //set application name
要修改模型连接字符串,请使用 EntityConnectionStringBuilder:
EntityConnectionStringBuilder entityConnectionBuilder = new
EntityConnectionStringBuilder();
entityConnectionBuilder.Metadata =
"res://*/PresentModel.csdl|res://*/PresentModel.ssdl|res://*/PresentModel.msl";
entityConnectionBuilder.Provider = "System.Data.SqlClient";
entityConnectionBuilder.ProviderConnectionString =
connectionStringBuilder.ConnectionString;
PresentModel 应随您的型号名称而变化。
最后使用此连接字符串创建模型上下文的新实例:
var entityContext = new
PresentModelConnectionString(entityConnectionBuilder.ConnectionString);