entity framework:无法加载指定的元数据资源
我决定将Entity Connection String
从app.config
移动到代码。 但是在设置之后如下:
public static string GetConnectionString() { string connection = ""; SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); sqlBuilder.DataSource = dbServer; sqlBuilder.InitialCatalog = dbInitialCatalog; sqlBuilder.IntegratedSecurity = false; sqlBuilder.UserID = dbUserName; sqlBuilder.Password = dbPasswWord; sqlBuilder.MultipleActiveResultSets = true; EntityConnectionStringBuilder entity = new EntityConnectionStringBuilder(); // entity.Name = "EntityBazaCRM"; entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl"; entity.Provider = "System.Data.SqlClient"; entity.ProviderConnectionString = sqlBuilder.ToString(); connection = entity.ToString(); return connection; }
我抛出exceptionUnable to load the specified metadata resource.
在.Designer.cs中。
/// /// Initialize a new EntityBazaCRM object. /// public EntityBazaCRM(string connectionString) : base(connectionString, "EntityBazaCRM") { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); }
如果我在我的Entity创建者中定义.Name,则会抛出另一个exception
"Other keywords are not allowed when the 'Name' keyword is specified." (System.ArgumentException) Exception Message = "Other keywords are not allowed when the 'Name' keyword is specified.", Exception Type = "System.ArgumentException"
我知道我遗漏了一些我必须改变的东西,以便自生成的代码使用新的连接字符串,但在哪里寻找它?
看完这篇答案文章和这篇博客后,我改变了:
entity.Metadata = @"res://*/Data.System.csdl|res://*/Data.System.ssdl|res://*/Data.System.msl";
至:
entity.Metadata = "res://*/";
它的工作原理:-)