我有一个在localhost上运行良好的codefirst应用程序,但是Azure数据库存在问题。问题是我不能下载创建数据库。它给出了超时错误:
System.Data.SqlClient.SqlException:'执行超时已过期。该 在完成操作或之前经过的超时时间 服务器没有响应。'
我在dbcontext构造函数中尝试了以下内容:
public ApplicationDbContext() : base("DbSporter") {
this.Database.CommandTimeout = 180;
Database.SetInitializer(new DropCreateDatabaseAlways<ApplicationDbContext>());
}
但我无法增加命令超时。它在大约30秒后仍然会出错。
我还尝试从web.config增加命令超时和连接超时。没有工作。
我尝试了两种不同的连接字符串:
1)connectionString="Data Source=tcp:sportit.database.windows.net;Initial Catalog=Sporter;Integrated Security=False;User ID=sporter;Password=*****;Connect Timeout=180;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
2)connectionString="Data Source=tcp:sportit.database.windows.net;Initial Catalog=Sporter;Integrated Security=False;User ID=sporter;Password=*****;Connect Timeout=180;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
他们两个都有相同的结果。 我是否应该知道如何使用EF codefirst创建azure数据库?
答案 0 :(得分:0)
众所周知,ADO.NET connectionString如下所示:
Server = tcp:{your_sql_server_name} .database.windows.net,1433; Initial Catalog = {your_database_name}; Persist Security Info = False; User ID = {your_username}; Password = {your_password}; MultipleActiveResultSets = False; Encrypt = True; TrustServerCertificate = False;连接超时= 30;
根据您的描述,我创建了我的MVC网站应用程序并将数据库初始化程序设置为DropCreateDatabaseAlways
来测试此问题。使用连接和命令的默认超时,我还可能遇到超时问题。然后,我尝试将超时增加到300秒,然后我可以删除并重新创建azure数据库。以下是代码段,您可以参考它们:
<强>的DbContext,初始化器,型号强>
{
public class BruceDBContext : DbContext
{
public BruceDBContext() : base("name=defaultConnectionString")
{
this.Database.CommandTimeout = 300;
}
public virtual DbSet<Company> Companies { get; set; }
}
public class BruceInitializer : DropCreateDatabaseAlways<BruceDBContext>
{
protected override void Seed(BruceDBContext context)
{
var companies = new List<Company>
{
new Company { Name = "AAA", City = "Eindhoven", Streetname="Street 12" },
new Company { Name = "BBB", City = "Rotterdam", Streetname = "Street 12" },
new Company { Name = "CCC", City = "Eindhoven", Streetname = "Street 12" },
new Company { Name = "EEE", City = "Eindhoven", Streetname = "Street 12" }
};
companies.ForEach(s => context.Companies.Add(s));
context.SaveChanges();
base.Seed(context);
}
}
[Table("Company")]
public class Company
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Streetname { get; set; }
}
}
Global.asax.cs&gt;的Application_Start 强>
Database.SetInitializer<BruceDBContext>(new BruceInitializer());
连接字符串
<add name="defaultConnectionString" connectionString="Server=tcp:brucesqlserver.database.windows.net,1433;Initial Catalog=brucetest4;Persist Security Info=False;User ID=brucechen;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=300;" providerName="System.Data.SqlClient"/>
<强>结果强>