2013-08-23 28 views
9

Tôi muốn đặt ConnectionTimeout thành một cái gì đó khác với mặc định, là 15 giây. Tôi đã thừa hưởng một số mã có sử dụng EntityFramework và app.config trông như thế này:Thiết lập ConnectionTimeout khi sử dụng EntityFramework

<configuration> 
    <configSections> 
     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" requirePermission="false" /> 
</configSections> 
<connectionStrings> 
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; ConnectionTimeout=30; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> 
</connectionStrings> 
<entityFramework> 
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
    <parameters> 
    <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; ConnectionTimeout=30; MultipleActiveResultSets=True" /> 
    </parameters> 
</defaultConnectionFactory> 
</entityFramework> 

tôi là người đã thêm sectino trong một nỗ lực để có được những điều làm việc. Tôi có thể nói rằng nó không hoạt động được thiết lập một breakpoint tại:

var adapter = (IObjectContextAdapter) this; 
var objectContext = adapter.ObjectContext; 
objectContext.CommandTimeout = CommandTimeoutSeconds; 
int test = objectContext.Connection.ConnectionTimeout; 

kiểm tra luôn luôn 15. Điều gì đang xảy ra? Ai đó có thể cho tôi biết làm thế nào để thiết lập ConnectionTimeout? Tôi đã thử cả "ConnectionTimeout" và "Connection Timeout" I.e. không có không gian so với không gian.

Ai đó có thể giúp tôi không? Tôi đang kéo tóc ra. Tôi chắc chắn đó là một sửa chữa đơn giản! Dave

Thông tin bổ sung. Để trả lời nhận xét, đây là lớp dẫn xuất DbContext của tôi ...

public class SessionDataContext : DbContext 
{ 
    // Command timeout (seconds) 
    private const int CommandTimeoutSeconds = 30; 

    /// <summary> 
    /// Constructor that takes db name. 
    /// The connection string and db itself is configured in the this project's app.config file 
    /// </summary> 
    /// <param name="dbName"></param> 
    public SessionDataContext(string dbName) : base(dbName) 
    { 
     Database.SetInitializer(new SessionDataContextInitializer()); 

     // Set timeout (based on code from http://stackoverflow.com/questions/6232633/entity-framework-timeouts) 
     var adapter = (IObjectContextAdapter) this; 
     var objectContext = adapter.ObjectContext; 
     objectContext.CommandTimeout = CommandTimeoutSeconds; 
     int test = objectContext.Connection.ConnectionTimeout; 
    } 

    /// <summary> 
    /// Session table's records 
    /// </summary> 
    public DbSet<Session> Sessions { get; set; } 

    /// <summary> 
    /// SessionType table's records 
    /// </summary> 
    public DbSet<SessionType> SessionTypes { get; set; } 
} 
+0

làm cách nào để bạn tạo lớp dẫn xuất DbContext? Bạn có truyền tên chuỗi kết nối ở đó không? – Pawel

+0

Xin chào Pawel, tôi đặt lớp DbContext được điều khiển trong câu hỏi. Cảm ơn bạn đã xem câu hỏi của tôi. – Dave

+0

Bạn có xem http://stackoverflow.com/questions/6232633/entity-framework-timeouts không? –

Trả lời

7

Đó là sự ngu xuẩn của tôi đã gây ra sự cố! Tôi đặt câu trả lời của tôi ở đây trong trường hợp bất kỳ ai trong tương lai có vấn đề này. Tất cả mọi thứ tôi gõ ở trên là chính xác và sẽ hoạt động tốt. Tuy nhiên, tệp app.config mà tôi đang xem nằm trong thư viện lớp (lớp DataAccess của chúng tôi). Trong thực tế, nó không được sử dụng ở tất cả và các thiết lập EntityFramework mặc định đang được sử dụng. Tôi là một trong những chắc chắn những gì đã dẫn tôi thử nó, nhưng tôi đã di chuyển các cài đặt app.config từ lớp DataAccess app.config đến app.config chính và tất cả đều làm việc rất đẹp. Về tất cả những gì tôi có thể nói trong phòng thủ của tôi ngoài việc tôi thừa kế mã là tôi không thấy rõ ràng rằng các giá trị trong app.config không được sử dụng và không gọi chúng hoặc sử dụng chúng trong mã của riêng mình. Thay vào đó, MultipleActiveResultSets và ConnectionTimeout được sử dụng bởi Khuôn khổ thực thể cơ bản.

Các vấn đề liên quan