문제

the problem is as follows; We have code that talks to many databases that all have the same or similar model.

The way that the code is structured is that each business project just has a data context defined of the tables it cares about (there are over a thousand). This means that we end up having a need to generate connection strings based on server information.

The problem becomes the metadata=res:///DataModel.XXXX.csdl|res:///DataModel.XXXX.ssdl|res://*/DataModel.XXXX.msl section; as this name is not standardized.

Is there a way to use the System.Data.Objects.ObjectContex combined with reflection to solve for XXXX.

the code we use that requires us to solve for that is as follows.

var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = (string.IsNullOrWhiteSpace(DatabaseServerUri) ? Uri : DatabaseServerUri) + (string.IsNullOrEmpty(SqlInstanceName) ? string.Empty : string.Format(@"\\{0}", SqlInstanceName));
scsb.InitialCatalog = MainDatabase; 
scsb.IntegratedSecurity = false;
scsb.Password = "stuff";
scsb.UserID = "morestuff";
scsb.ConnectTimeout = 3600;

var builder = new EntityConnectionStringBuilder();
builder.Metadata = "res://*/DataModel.XXXX.csdl|res://*/DataModel.XXXX.ssdl|res://*/DataModel.XXXX.msl"; 
builder.Provider = "System.Data.SqlClient"; 
builder.ProviderConnectionString = scsb.ConnectionString;

return builder.ConnectionString;
도움이 되었습니까?

해결책

Extending my comment to an answer

Model and Mapping files are normally embedded in the assembly (where your ObjectContext is) so you can provide that assembly name to read them from assembly's embedded resources. To achieve this you could do the following:

builder.Metadata = string.Format("res://{0}", typeof(MyObjectContext).Assembly.FullName);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top