Firstly I had to create myself a new class in my DB ORM assembly
Then I had to add a reference to the System.Data.Entity assembly.
Then added this static method to my static class (including the namespaces required)
using System.Data.SqlClient;
using System.Data.EntityClient;
namespace MY_DB_ORM
{
static class MY_DB
{
public static string ConnectionString()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
// Set the properties for the data source.
sqlBuilder.DataSource = "MY_SERVER_IP,1433";
sqlBuilder.InitialCatalog = "MY_DB_NAME";
sqlBuilder.UserID = "MY_USERID";
sqlBuilder.Password = "MY_PASSWORD";
sqlBuilder.IntegratedSecurity = false;
// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = "System.Data.SqlClient";
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/MY_CONTEXT_CLASS_NAME.csdl|
res://*/MY_CONTEXT_CLASS_NAME.ssdl|
res://*/MY_CONTEXT_CLASS_NAME.msl";
return entityBuilder.ToString();
}
}
}
Then in my context class I could alter the constructor to use this method..
public partial class MY_CONTEXT_CLASS_NAME : DbContext
{
public MY_CONTEXT_CLASS_NAME_CONSTRUCTOR()
: base(MY_DB.ConnectionString())
{
}
Now I am able to remove the connection string to my model from the app.config file where it was held in clear text for all to see!
https://community.spiceworks.com/topic/815431-ef-can-t-get-it-to-work
Không có nhận xét nào:
Đăng nhận xét