实体框架 - 不支持关键字:“元数据”

发布于 2024-12-04 03:20:44 字数 1154 浏览 1 评论 0原文

我有以下类:

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=&quot;data source=Bob-PC;initial catalog=Entities;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2024-12-11 03:20:44

DbContext 已经有一个接受连接字符串或名称的构造函数。您可以修改现有的上下文类以包含接受连接字符串参数并调用base(connectionStringOrName)的构造函数吗?

因此,上下文将类似于:

public class SomeContext : DbContext, IObjectContextAdapter
{
    public SomeContext(string connectionStringOrName)
        : base (connectionStringOrName)
    {
    }

    // Rest of the implementation...
}

然后 EFRepository的构造函数将如下所示:

public EFRepository(string connectionStringName)
{
    context = new TContext(connectionStringName);
}

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 call base(connectionStringOrName)?

So a context would look something like:

public class SomeContext : DbContext, IObjectContextAdapter
{
    public SomeContext(string connectionStringOrName)
        : base (connectionStringOrName)
    {
    }

    // Rest of the implementation...
}

And then the constructor of EFRepository<TContext> would look like:

public EFRepository(string connectionStringName)
{
    context = new TContext(connectionStringName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文