Created a model (Code fluent):

public PersonConfig() { HasKey(id => id.PersonID); Property(name => name.Name).IsRequired().HasMaxLength(50); } public class Person { public int PersonID { get; set; } public string Name { get; set; } } class CodeContext: DbContext { public CodeContext(string connectionString) : base( connectionString) { } public DbSet<Person> Person{ get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new PersonConfig()); base.OnModelCreating(modelBuilder); } } 

After that, in the viewmodel I send the line, I do the initialization:

  try { using (var cnxt = new CodeContext(connectionString)) { cnxt.Database.Initialize(true); } } catch (Exception ex) { MessageBox.Show("Произошла ошибка с сервером"); } 

The problem is this: if I appeal to the server, which is, then everything is fine, after working with it (if there is no database, it creates, if it does, then it uses it). But if the server string is incorrect, then the download goes about 01:07 minutes, and only after it moves to catch (exception ex). How to reduce time to the server ?? Or how to check if the server is entered correctly. Over time, I tried to write in CodeContext for 30 seconds, but it does not work .:

 this.Database.CommandTimeout = 30; 

How to solve this problem? MB, I incorrectly do the initialization, although it doesn’t work differently, and even if the server string is correct, the call goes without problems (but the appeal to the correct server is initially about 8 seconds, which seems to be a lot, because I’m contacting myself )?

    1 answer 1

    You use the command timeout , which determines the timeout for the query (which will be valid after the connection with the database is established). In your case, you need a connection timeout , which indicates the time to wait for the connection to the database, or rather, how long this wait will be interrupted and an error will be generated.

    There is a connection between the timeline and the error.

    you can specify it directly in the database connection string

     Connection Timeout=60; 

    But, this is true if I am accessing a non-existent server, if there is a server (I tried to access myself from non-administrator rights, to the administrator, which is just not available), and the access time was 6 seconds! What is the time algorithm? How does he choose how to be now?

    The Connection Timeout parameter specifies how long the SQL service will respond to the connection attempt . In other words, this attempt must first be made by reaching the SQL server. This setting does not adjust the time for network delays, DNS problems, or other problems.

    • I added to the line that you said: "Data Source=tempServer;Initial Catalog = database;Integrated Security=True;Connect Timeout=30;Application Name=EntityFramework" - but now the download is as if eternal. There is a call to the server (based on an exception) after OnModelCreating , and this error is displayed (which should be if the server is entered incorrectly): (provider: Named Pipes Provider, error: 40 - Не удалось открыть подключение к SQL Server) . But I would like it in 30 seconds, not in 2 minutes? - Bruceee
    • Strange behavior. Try adding the command timeout ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 35; - Artyom Okonechnikov
    • @Bruceee, helped you? If not, then I will have to delete the answer as useless. - Artyom Okonechnikov
    • What you suggested above (with objectcontext ) also, however, I conducted an experiment, set the value to connect timeout from 1 to 8 and measured the access time to the server each time, and noticed an interesting pattern (but she surprised me why? ?): 1 - 10 секунд; 2 - 10 секунд; 3 - 20 секунд; 4 - 20 секунд; 5 - 30 секунд; 6 - 30 секунд; 7 - 40 секунд; 8 - 40 секунд; 1 - 10 секунд; 2 - 10 секунд; 3 - 20 секунд; 4 - 20 секунд; 5 - 30 секунд; 6 - 30 секунд; 7 - 40 секунд; 8 - 40 секунд; .... (2*n - 1; 2*n) = 10*n секунд ; I measured after the very first appeal OnModelCreating . You do not know by chance why? - Bruceee
    • But, this is true if I am accessing a non-existent server, if there is a server (I tried to access myself from non-administrator rights, to the administrator, which is just not available), and the access time was 6 seconds! What is the time algorithm? How does he choose how to be now? - Bruceee