【问题标题】:Does Entity Framework support ApplicationIntent=ReadOnly in SQLClient connection string实体框架是否支持 SQLClient 连接字符串中的 ApplicationIntent=ReadOnly
【发布时间】:2012-12-13 18:33:32
【问题描述】:

实体框架是否支持 SQLClient 连接字符串中的ApplicationIntent=ReadOnly?它参考了AlwaysOn Availability Groups (SQL Server 2012)Read-Only access on an Availability Replica 功能。

【问题讨论】:

    标签: entity-framework sql-server-2012


    【解决方案1】:

    到目前为止,我发现的唯一方法是直接使用连接字符串创建上下文,而不是使用连接字符串名称。这样你就可以附加ApplicationIntent

    string connectionString = string.Format("{0}; ApplicationIntent=READONLY", ConfigurationManager.ConnectionStrings["AppContext"].ConnectionString);
    
    using (AppContext context = new AppContext(connectionString))
    {
        var q = from row in context.table
                select row.id;
    }
    

    【讨论】:

    • 作为这里的注释,我已经取得了巨大的成功,使用了一个与此类似的方法,但使用了一个静态“工厂”类,该类将一个布尔值作为参数,用于判断是否需要写访问,如果为 false ,返回包含 ApplicationIntent 的 Context 实例。这使得代码更简洁(不是在每个类中构建连接字符串),并且仍然实现了目标。类似 Factory.GetDataContext(bool needsWrite=false)
    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2011-11-04
    • 1970-01-01
    相关资源
    最近更新 更多