.net 实体框架 4.1:如何按对象 ID 删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6948911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework 4.1: How do I delete by object Id
提问by Paul
I would like to know how to delete an object from Entity Framework 4.1 without first having to load the object from the database. I have found theseother2 answers on Stack Overflow, but they do not pertain to EF 4.1
我想知道如何从 Entity Framework 4.1 中删除对象,而不必先从数据库加载对象。我发现这些其他的堆栈溢出2个回答,但他们不属于EF 4.1
I have tried the following code but it does not work
我已经尝试了以下代码,但它不起作用
public void DeleteCar(int carId)
{
var car = new Car() { Id = carId };
_dbContext.Cars.Attach(car);
_dbContext.Cars.Remove(car);
_dbContext.SaveChanges();
}
I want to avoid the code below.
我想避免下面的代码。
public void DeleteCar(int carId)
{
var car = context.Cars.Find(carId);
_dbContext.Cars.Remove(car);
_dbContext.SaveChanges();
}
And I do not want to call a stored procedure or execute raw sql.
而且我不想调用存储过程或执行原始 sql。
回答by agradl
I use the following for my deletes, works great.
我使用以下内容进行删除,效果很好。
public virtual ActionResult Delete(int commentID)
{
var c = new Comment(){CommentID = commentID};
db.Entry(c).State= EntityState.Deleted;
db.SaveChanges();
return RedirectToAction(MVC.Blog.AdminComment.Index());
}


回答by Slauma
Just to convince you that your first code snippet must work here is a simple example you can copy, paste and test:
为了让您相信您的第一个代码片段必须在这里工作,这是一个您可以复制、粘贴和测试的简单示例:
- Create a new console application project (.NET 4)
- Add reference to
EntityFramework.dll(EF 4.1) - Delete the content of
Program.csand paste in the following:
- 创建一个新的控制台应用程序项目 (.NET 4)
- 添加对
EntityFramework.dll(EF 4.1)的引用 - 删除以下内容
Program.cs并粘贴:
->
->
using System.Data.Entity;
namespace EFDeleteTest
{
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Car> Cars { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Database with name "EFDeleteTest.MyContext"
// will be created automatically in SQL Server Express
int carId = 0;
using (var context = new MyContext())
{
var car = new Car { Name = "Test" };
context.Cars.Add(car);
context.SaveChanges();
carId = car.Id;
}
//Make breakpoint here and check that the car is indeed in the DB
using (var context = new MyContext())
{
var car = new Car() { Id = carId };
context.Cars.Attach(car);
context.Cars.Remove(car);
context.SaveChanges();
}
//Make breakpoint here and check that the car
//indeed has been deleted from DB
}
}
}
If you have a SQL Server Express DB it will automatically create the DB.
如果您有 SQL Server Express DB,它将自动创建该 DB。
Perhaps this might help you to compare with your code because it looks as if something not visible in your code fragments is causing the different behaviour you describe.
也许这可能会帮助您与您的代码进行比较,因为它看起来好像您的代码片段中不可见的某些东西导致了您描述的不同行为。
回答by Milox
use stub entities:
使用存根实体:
public void DeleteCar(int carId)
{
var car = new Car() { Id = carId };
_dbContext.AttachTo("Cars",car);
_dbContext.DeleteObject(car);
_dbContext.SaveChanges();
}
reference:
参考:
回答by Boris Lipschitz
The following code works well for me:
以下代码对我很有效:
var c = db.Set<T>().Find(id);
db.Entry(c).State = EntityState.Deleted;
db.SaveChanges();
If this object wasn't tracked previously by the DbContext then it would hit the database, otherwise it would find it in memory.
如果 DbContext 先前未跟踪此对象,则它会访问数据库,否则会在内存中找到它。

