1)Just find out the specific instance of the entity model and then pass this as a parameter into "DeleteObject" method——
object deletedProduct;
// Define the key of the product to delete.
EntityKey productKey =
new EntityKey("AdventureWorksEntities.Products",
"ProductID", productId);
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Get the object to delete with the specified key.
if (context.TryGetObjectByKey(productKey, out deletedProduct))
{
try
{
// Delete the object with the specified key
// and save changes to delete the row from the data source.
context.DeleteObject(deletedProduct);
context.SaveChanges();
}
catch (OptimisticConcurrencyException ex)
{
throw new InvalidOperationException(string.Format(
"The product with an ID of '{0}' could not be deleted.\n"
+ "Make sure that any related objects are already deleted.\n",
productKey.EntityKeyValues[0].Value), ex);
}
}
else
{
throw new InvalidOperationException(string.Format(
"The product with an ID of '{0}' could not be found.\n"
+ "Make sure that Product exists.\n",
productKey.EntityKeyValues[0].Value));
}
}
2)Please do execution of Delete SQL:
DbContext ctx = ... get your DbContext somehow...
ctx.Database.ExecuteSqlCommand("DELETE FROM xxx WHERE xxxId= 1");
context.TryGetObjectByKey(productKey,out deletedProduct) does not compile in .net 4.5
context.DeleteObject(deletedProduct); does not compile in .net 4.5
ArneATK
Member
3 Points
25 Posts
How do I delete a record with Entity Framework 5
Sep 07, 2012 12:39 PM|LINK
I upgraded my code from .Net 4 to .Net 4.5. Now the DeleteObject and commandtimeout no longer compiles
How do I delete a record with Entity Framework 5
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How do I delete a record with Entity Framework 5
Sep 09, 2012 01:15 AM|LINK
Hi,
Two ways:
1)Just find out the specific instance of the entity model and then pass this as a parameter into "DeleteObject" method——
object deletedProduct; // Define the key of the product to delete. EntityKey productKey = new EntityKey("AdventureWorksEntities.Products", "ProductID", productId); using (AdventureWorksEntities context = new AdventureWorksEntities()) { // Get the object to delete with the specified key. if (context.TryGetObjectByKey(productKey, out deletedProduct)) { try { // Delete the object with the specified key // and save changes to delete the row from the data source. context.DeleteObject(deletedProduct); context.SaveChanges(); } catch (OptimisticConcurrencyException ex) { throw new InvalidOperationException(string.Format( "The product with an ID of '{0}' could not be deleted.\n" + "Make sure that any related objects are already deleted.\n", productKey.EntityKeyValues[0].Value), ex); } } else { throw new InvalidOperationException(string.Format( "The product with an ID of '{0}' could not be found.\n" + "Make sure that Product exists.\n", productKey.EntityKeyValues[0].Value)); } }2)Please do execution of Delete SQL:
DbContext ctx = ... get your DbContext somehow... ctx.Database.ExecuteSqlCommand("DELETE FROM xxx WHERE xxxId= 1");ArneATK
Member
3 Points
25 Posts
Re: How do I delete a record with Entity Framework 5
Sep 10, 2012 11:51 AM|LINK
I don't think that compiles with Entity framework 5.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How do I delete a record with Entity Framework 5
Sep 11, 2012 01:11 AM|LINK
Hi, which method do you think cannot pass compiles?Both?
ArneATK
Member
3 Points
25 Posts
Re: How do I delete a record with Entity Framework 5
Sep 11, 2012 12:49 PM|LINK
context.TryGetObjectByKey(productKey,out deletedProduct) does not compile in .net 4.5
context.DeleteObject(deletedProduct); does not compile in .net 4.5
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How do I delete a record with Entity Framework 5
Sep 12, 2012 12:15 AM|LINK
Hi again,
If you are using Code-First,I think you can do deleting to this problem——
var c = new ClassModel(){Property= Value}; db.Entry(c).State= EntityState.Deleted; db.SaveChanges();ArneATK
Member
3 Points
25 Posts
Re: How do I delete a record with Entity Framework 5
Sep 12, 2012 01:04 PM|LINK
The DbSet.Remove method works for removing records.
npetroff
Member
36 Points
18 Posts
Re: How do I delete a record with Entity Framework 5
Dec 19, 2012 02:13 PM|LINK
The .Remove() method does not work with non-nullable foreign-key constraints.
ArneATK
Member
3 Points
25 Posts
Re: How do I delete a record with Entity Framework 5
Dec 19, 2012 02:19 PM|LINK
Do you mean to remove a record that has a child record is a problem? or is removing a child record a problem?
npetroff
Member
36 Points
18 Posts
Re: How do I delete a record with Entity Framework 5
Dec 19, 2012 02:21 PM|LINK
If i try to remove a child record that has a non-nullable foreign-key constraint (ParentID) db.SaveChanges() will throw an exception.