使用实体框架连接数据库。
根据UI中的用户选择动态生成连接字符串。
场景是用户从UI中选择一些连接并基于该选择实体框架必须连接数据库查询。
以下代码用于生成实体连接字符串。但它没有按原样运作。
用于传递连接字符串的实体部分类。
public partial class Entities
{
public Entities(string connectionName)
: base(connectionName)
{
}
}
public static void GenerateEntityConnectionString(string serverName, string databaseName, string providerName,
string databaseUserId, string databasePassword, short? databaseTimeout)
{
string connnectionString = string.Empty;
// Specify the provider name, server and database.
//string providerName = "System.Data.SqlClient";
//string serverName = ".";
//string databaseName = "AdventureWorks";
// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
new SqlConnectionStringBuilder();
// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = false;
sqlBuilder.UserID = databaseUserId;
sqlBuilder.Password = databasePassword;
sqlBuilder.MultipleActiveResultSets = true;
sqlBuilder.ConnectTimeout = Convert.ToInt32(databaseTimeout);
// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = providerName;
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/EF.EntitiesModal.csdl|res://*/EF.EntitiesModal.ssdl|res://*/EF.EntitiesModal.msl";
//entityBuilder.Metadata = @"res://*/";
connnectionString = entityBuilder.ConnectionString;
ConnectionString = connnectionString;
}
更新 在使用动态生成的连接字符串时,实体框架会生成错误,即无效的对象名称。虽然表存在于数据库中。
查看下面的图片以便更好地理解。
表格下面有检查图片:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection,Action
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔& dataReady)at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()at System.Data.SqlClient.SqlDataReader.get_MetaData()at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString)at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32超时,任务& task,Boolean asyncWrite,SqlDataReader ds,Boolean describeParameterEncryptionRequest)at System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
1 c)at System.Data.Entity.Infrastructure.Interception.InternalDispatcher
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func
3操作,TInterceptionContext interceptionContext, 行动3 executing, Action
3已执行) System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(的DbCommand 命令,DbCommandInterceptionContext interceptionContext)at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(的CommandBehavior 行为) System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
在 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior行为)