问题
I trying to create a application with use some Entity Framework and some old DataSets but I only have a dynamic connection string using entity and need remove metadata at runtime to create a connection string to my database. Anyone know a way to remove all metadata from Entity Framework Connection
I have this
metadata=res://*/Models.SiteModel.csdl|res://*/Models.SiteModel.ssdl|res://*/Models.SiteModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(local);Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=P@ssw0rd;MultipleActiveResultSets=True"
and need only this:
Data Source=(local);Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=P@ssw0rd;MultipleActiveResultSets=True
回答1:
You can use the EntityConnectionStringBuilder
class:
string connectionString = "metadata=res://*/Models.SiteModel.csdl|res://*/Models.SiteModel.ssdl|res://*/Models.SiteModel.msl;provider=System.Data.SqlClient;provider connection string=\"Data Source=(local);Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=P@ssw0rd;MultipleActiveResultSets=True\"";
EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder(connectionString);
builder.Metadata = null;
connectionString = builder.ConnectionString;
回答2:
A better property to use is builder.ProviderConnectionString
rather than stripping the Metadata from the builder to get build.ConnectiongString
.
来源:https://stackoverflow.com/questions/6030430/regular-expression-for-remove-metadata