[英]Asp.Net Core 3.1 Data is retrieved but not saved into database
I am creating an Asp.Net core web application.我正在创建一个 Asp.Net 核心 Web 应用程序。 I have successfully connected database to my app.
我已成功将数据库连接到我的应用程序。 I can run all the migration successfully and even retrieve data from database.
我可以成功运行所有迁移,甚至可以从数据库中检索数据。 But when I try to save data using my DbContext data is not saved in the database.
但是当我尝试使用我的 DbContext 保存数据时,数据没有保存在数据库中。 Here is my code
这是我的代码
Category Model Class类别模型类
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
Category Controller类别控制器
public class CategoryController : Controller
{
private readonly AppDbContext dbContext;
public CategoryController(AppDbContext dbContext)
{
this.dbContext = dbContext;
}
[HttpGet]
public IActionResult Index()
{
var categories = dbContext.Categories.ToList();
return View(categories);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Category category)
{
if (ModelState.IsValid)
{
dbContext.Categories.Add(category);
return RedirectToAction(nameof(Index));
}
return View();
}
}
View看法
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
DbContext Class数据库上下文类
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
}
and finally my ConfigureServices Method in Startup file最后我在启动文件中的ConfigureServices 方法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<AppDbContext>(options => options
.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("AppDbContext")));
}
I successfully get data in the Index method that I have manually stored in database but cannot save from a form.我成功地在 Index 方法中获取了我手动存储在数据库中但无法从表单中保存的数据。
It is necessary to call SaveChanges()
method of EntityFramework to save data:需要调用EntityFramework的
SaveChanges()
方法来保存数据:
dbContext.Categories.Add(category);
dbContext.SaveChanges();
It is necessary to call SaveChanges
after creating, updating, deleting operations.创建、更新、删除操作后需要调用
SaveChanges
。
In Entity Framework, the
DbContext.SaveChanges()
method saves all changes made in the context of the database.在实体框架中,
DbContext.SaveChanges()
方法保存在数据库上下文中所做的所有更改。
So after adding, updating or deleting data you need call SaveChages()
method:所以在添加、更新或删除数据后,您需要调用
SaveChages()
方法:
dbContext.Categories.Add(category);
dbContext.SaveChanges();
for knowing more about SaveChange()
you can follow this link .要了解有关
SaveChange()
更多信息,您可以点击此链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.