实体框架 - 不支持关键字:“元数据”
我有以下类:
public class EFRepository<TContext> : IDisposable where TContext : DbContext, IObjectContextAdapter, new()
{
private TContext context;
public EFRepository(string connectionStringName)
{
context = new TContext();
context.Database.Connection.ConnectionString =
ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}
}
具有以下连接字符串:
<connectionStrings>
<add name="EntitiesConnection" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=Bob-PC;initial catalog=Entities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
像这样调用:
var Entities = new EFRepository<EntitiesConnection>("EntitiesConnection");
这会在主题行中引发错误。我已经看到使用 EntityStringBuilder 的解决方案,但是 Connection 属性是只读的。关于如何实现这项工作有什么想法吗?
谢谢,
鲍勃
I have the following class:
public class EFRepository<TContext> : IDisposable where TContext : DbContext, IObjectContextAdapter, new()
{
private TContext context;
public EFRepository(string connectionStringName)
{
context = new TContext();
context.Database.Connection.ConnectionString =
ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
}
}
with the following connection string:
<connectionStrings>
<add name="EntitiesConnection" connectionString="metadata=res://*/EntityModel.csdl|res://*/EntityModel.ssdl|res://*/EntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=Bob-PC;initial catalog=Entities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Being called like this:
var Entities = new EFRepository<EntitiesConnection>("EntitiesConnection");
Which throws the error in the subject line. I've seen the solutions using the EntityStringBuilder, however the Connection property is read only. Any ideas on how to make this work?
Thanks,
Bob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DbContext
已经有一个接受连接字符串或名称的构造函数。您可以修改现有的上下文类以包含接受连接字符串参数并调用base(connectionStringOrName)
的构造函数吗?因此,上下文将类似于:
然后 EFRepository的构造函数将如下所示:
DbContext
already has a constructor that accepts a connection string or name. Can you modify your existing context classes to include a constructor that accepts a connection string parameter and callbase(connectionStringOrName)
?So a context would look something like:
And then the constructor of
EFRepository<TContext>
would look like: