EF状态System.Data.EntityState

一. System.Data.EntityState一共有五种状态

分别是Added,Deleted,Modified,UnChanged,Detached下面给一个简单的解释

System.Data.EntityState.Added

在上下文中,而且对象是需要添加的状态

System.Data.EntityState.Deleted

在上下文中,而且对象是需要删除的状态

System.Data.EntityState.Modified

在上下文中,而且对象是需要修改的状态

System.Data.EntityState.UnChanged

在上下文中,对象没有任何状态

System.Data.EntityState.Detached

没有在上下文中,和ef上下文没有关联的状态

二. System.Data.EntityState.Added

在这里插入图片描述
调用oae.Users.Add方法后对象属于EntityState.Added状态

三. System.Data.EntityState.Deleted

在调用EntityState.Deleted之前,该实体必须是Unchanged状态。

调用Attach方法后对象属于EntityState.Unchanged状态,当然我们还可以使用oae.Entry(item).State = EntityState.Unchanged;方式把对象以Unchanged状态附加到上下文,道理都一样
在这里插入图片描述
调用Remove方法后对象属于EntityState.Deleted状态

四. System.Data.EntityState.Modified

在调用EntityState.Modified之前,该实体必须是Unchanged状态。

调用Attach方法后对象属于EntityState.Unchanged状态,当然我们还可以使用oae.Entry(item).State = EntityState.Unchanged;方式把对象以Unchanged状态附加到上下文,道理都一样
在这里插入图片描述
调用oae.Entry(Users).State = EntityState.Modified对象属于EntityState.Modified状态。

五. System.Data.EntityState.UnChanged

①查询出来的对象属于EntityState.UnChanged状态,注意不加AsNoTracking()
在这里插入图片描述
②自上次调用 System.Data.Objects.ObjectContext.SaveChanges() 方法后属于EntityState.UnChanged状态

③new的对象调用Attach方法后对象属于EntityState.Unchanged状态,当然我们还可以使用oae.Entry(item).State = EntityState.Unchanged;方式把对象以Unchanged状态附加到上下文,道理都一样
在这里插入图片描述

六. System.Data.EntityState.Detached

①new 实体对象()

②查询数据库的对象的时候加AsNoTracking()

七.注意:本地自己创建的Dto数据不是当前上下文的模型的一部分。更没有EntityState状态一说。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET MVC 是一种基于模型-视图-控制器 (MVC) 架构的 Web 应用程序框架。它提供了一种创建 Web 应用程序的模式,可以使开发人员将应用程序的逻辑、布局和用户界面分开。 在 ASP.NET MVC 中,常见的增删改查操作可以使用以下步骤完成: 1. 创建模型类:定义要操作的实体类。 2. 创建控制器类:定义处理请求和响应的代码,包括数据操作和视图渲染。 3. 创建视图:定义呈现数据的 HTML 界面。 4. 实现数据操作:可以使用 Entity Framework 或者其他 ORM 框架来实现数据库的增删改查操作。 下面是一个简单的示例,演示如何在 ASP.NET MVC 中实现增删改查操作: 1. 创建模型类: ``` public class Book { public int BookId { get; set; } public string Title { get; set; } public string Author { get; set; } public decimal Price { get; set; } } ``` 2. 创建控制器类: ``` public class BookController : Controller { // 显示书籍列表 public ActionResult Index() { var books = db.Books.ToList(); return View(books); } // 添加书籍 public ActionResult Add() { return View(); } [HttpPost] public ActionResult Add(Book book) { db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } // 修改书籍 public ActionResult Edit(int id) { var book = db.Books.Find(id); return View(book); } [HttpPost] public ActionResult Edit(Book book) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } // 删除书籍 public ActionResult Delete(int id) { var book = db.Books.Find(id); db.Books.Remove(book); db.SaveChanges(); return RedirectToAction("Index"); } private ApplicationDbContext db = new ApplicationDbContext(); } ``` 3. 创建视图: ``` @model IEnumerable<Book> <h2>书籍列表</h2> <p> @Html.ActionLink("添加书籍", "Add") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Author) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Author) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.ActionLink("编辑", "Edit", new { id=item.BookId }) | @Html.ActionLink("删除", "Delete", new { id=item.BookId }) </td> </tr> } </table> ``` ``` @model Book <h2>添加书籍</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="添加" class="btn btn-default" /> </div> </div> } <div> @Html.ActionLink("返回列表", "Index") </div> ``` ``` @model Book <h2>编辑书籍</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="保存" class="btn btn-default" /> </div> </div> } <div> @Html.ActionLink("返回列表", "Index") </div> ``` 4. 实现数据操作: 可以使用 Entity Framework 来实现数据库的增删改查操作,具体代码可以参考以下示例: ``` public class ApplicationDbContext : DbContext { public DbSet<Book> Books { get; set; } } ``` 注意,在使用 Entity Framework 之前,还需要进行一些配置,例如在 Web.config 中添加连接字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值