C# 如何使用DBContext在本地sql数据库中永久保存数据?
C# 如何使用DBContext在本地sql数据库中永久保存数据?,c#,sql-server,visual-studio-2019,localdb,C#,Sql Server,Visual Studio 2019,Localdb,我正在试图理解为什么当我的应用程序关闭时,我的数据没有保存在本地SQL db中。 我正在运行一个连接到SQL server的简单控制台应用程序。 这是我的连接字符串: Data Source=(localdb)\mssqllocaldb;Initial Catalog=SwagAiDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=R
我正在试图理解为什么当我的应用程序关闭时,我的数据没有保存在本地SQL db中。
我正在运行一个连接到SQL server的简单控制台应用程序。
这是我的连接字符串:
Data Source=(localdb)\mssqllocaldb;Initial Catalog=SwagAiDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
我创建了以下类:
public partial class SwagAiDbContext : DbContext
{
public SwagAiDbContext()
{
}
public SwagAiDbContext(DbContextOptions<SwagAiDbContext> options)
: base(options)
{
}
public virtual DbSet<Company> Company { get; set; }
public virtual DbSet<IndustrySection> IndustrySection { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=SwagAiDb;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>(entity =>
{
entity.Property(e => e.CompanyName).IsRequired();
entity.Property(e => e.Url).IsRequired();
});
modelBuilder.Entity<IndustrySection>(entity =>
{
entity.HasKey(e => e.IndustryCode)
.HasName("PK_IndustryCode");
entity.Property(e => e.IndustryCode).ValueGeneratedNever();
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
数据被添加到数据库中。一切看起来都很好。但当我重新启动我的应用程序时,数据库是空的。所有表都存在,但找不到数据。请帮助我。检查您是否正在使用DropCreateDatabaseAlways数据库初始值设定项选项 编辑: 另一种尝试方法: 将连接字符串移动到应用程序配置
<connectionStrings>
<add name="SwagAiDbContext"
connectionString="data source=(localdb)\mssqllocaldb;Initial Catalog=SwagAiDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
然后像这样重写dbcontext:
public SwagAiDbContext()
: base("name=SwagAiDbContext")
{
}
public virtual DbSet<Company> Company { get; set; }
public virtual DbSet<IndustrySection> IndustrySection { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>(entity =>
{
entity.Property(e => e.CompanyName).IsRequired();
entity.Property(e => e.Url).IsRequired();
});
modelBuilder.Entity<IndustrySection>(entity =>
{
entity.HasKey(e => e.IndustryCode)
.HasName("PK_IndustryCode");
entity.Property(e => e.IndustryCode).ValueGeneratedNever();
});
}
publicswagaidbcontext()
:base(“name=SwagAiDbContext”)
{
}
公共虚拟数据库集公司{get;set;}
公共虚拟数据库集IndustrySection{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity(Entity=>
{
Property(e=>e.CompanyName).IsRequired();
Property(e=>e.Url).IsRequired();
});
modelBuilder.Entity(Entity=>
{
entity.HasKey(e=>e.IndustryCode)
.HasName(“PK_行业代码”);
entity.Property(e=>e.IndustryCode).ValueGeneratedNever();
});
}
检查您是否正在使用DropCreateDatabaseAlways数据库初始值设定项选项
编辑:
另一种尝试方法:
将连接字符串移动到应用程序配置
<connectionStrings>
<add name="SwagAiDbContext"
connectionString="data source=(localdb)\mssqllocaldb;Initial Catalog=SwagAiDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
然后像这样重写dbcontext:
public SwagAiDbContext()
: base("name=SwagAiDbContext")
{
}
public virtual DbSet<Company> Company { get; set; }
public virtual DbSet<IndustrySection> IndustrySection { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>(entity =>
{
entity.Property(e => e.CompanyName).IsRequired();
entity.Property(e => e.Url).IsRequired();
});
modelBuilder.Entity<IndustrySection>(entity =>
{
entity.HasKey(e => e.IndustryCode)
.HasName("PK_IndustryCode");
entity.Property(e => e.IndustryCode).ValueGeneratedNever();
});
}
publicswagaidbcontext()
:base(“name=SwagAiDbContext”)
{
}
公共虚拟数据库集公司{get;set;}
公共虚拟数据库集IndustrySection{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity(Entity=>
{
Property(e=>e.CompanyName).IsRequired();
Property(e=>e.Url).IsRequired();
});
modelBuilder.Entity(Entity=>
{
entity.HasKey(e=>e.IndustryCode)
.HasName(“PK_行业代码”);
entity.Property(e=>e.IndustryCode).ValueGeneratedNever();
});
}
尝试使用提交更改()
而不是保存更改()
尝试使用SubmitChanges()
而不是SaveChanges()
删除了Visual c++标记:这与c++无关。您可以提出您的问题,以包含上下文的构造函数吗?第一个问题:您是否正在访问数据库?第二个问题:在这种情况下,您正在执行什么查询(所有内容,包括参数)?第三个问题:您是否遇到错误?问题可能是在启动时,而不是在dbcontext中。或者在申请结束时。谢谢你们的快速回复。我已经为我的类添加了完整的代码。我可以访问数据库。我可以在调试器中看到新记录,并且没有错误。已删除Visual c++标记:这与c++无关。您能否提出包含上下文构造函数的问题?第一个问题:您是否正在访问数据库?第二个问题:在这种情况下,您正在执行什么查询(所有内容,包括参数)?第三个问题:您是否遇到错误?问题可能是在启动时,而不是在dbcontext中。或者在申请结束时。谢谢你们的快速回复。我已经为我的类添加了完整的代码。我可以访问数据库。我可以在调试器中看到新记录,没有错误。SubmitChanges()
方法不存在SubmitChanges()
方法不存在此选项是否应显示在构造函数中?我用的是空的one@MayaStern是的,在构造函数和右边,您使用的是一个空的构造函数。检查使用示例尝试的编辑plsI,但我的基类DBContext
在其构造函数中未接收字符串参数。这是一个系统类,不是我的。我真的很感谢你的帮助哦,对不起,没有意识到你在EFCore上,这个选项应该出现在构造函数中吗?我用的是空的one@MayaStern是的,在构造函数和右边,您使用的是一个空的构造函数。检查使用示例尝试的编辑plsI,但我的基类DBContext
在其构造函数中未接收字符串参数。这是一个系统类,不是我的。我真的很感谢你的帮助哦,对不起,我不知道你在EFCore上,