EntityFramework - Cannot Delete Entity using DbSet [duplicate] - c#

This question already has answers here:
The object cannot be deleted because it was not found in the ObjectStateManager in entity framework 5
(5 answers)
Closed 8 years ago.
I am trying to delete an object from my database using EntityFramework's DbSet. The code is as follows:
var dbObject = FindById(id);
_masterDb.DbTable.Remove(dbObject);
_masterDb.SaveChanges();
I get the following error: "The object cannot be deleted because it was not found in the ObjectStateManager."
Please can someone tell me what I am doing wrong?

Possibly the entity is not attached to the same context.
Does this work:
var dbObject = FindById(id);
_masterDb.DbTable.Attach(dbObject);
_masterDb.DbTable.Remove(dbObject);
_masterDb.SaveChanges();

Related

"System.NullReferenceException" Error While Using Linq [duplicate]

This question already has answers here:
EF LINQ include multiple and nested entities
(7 answers)
Closed 1 year ago.
I'm trying to learn one to many relations with EF Core.
In summary: I have two tables; Artists and Songs.
But when I try to reach songs which belongs that artist I am getting error.
Why I'm getting null reference error?
You have to include the related entity you want in the list manually, include Songs like the following way:
var theArtist = DB.Artists.Include("Songs").FirstOrDefault(a => a.ID==1);

CannotDelete Entity in Database [duplicate]

This question already has answers here:
The object cannot be deleted because it was not found in the ObjectStateManager
(10 answers)
Closed 8 years ago.
Here is my Code,
db.Set<T>().Remove(item);
db.SaveChanges();
And i got this error.
{"The object cannot be deleted because it was not found in the ObjectStateManager."}
i tried many ways to fix that problem, but i couldn't. How can id fix ?
thank you all.
Most likely you are trying to remove an object that doesn't come from your DbContext. For example if your object comes from a web form in ASP .Net MVC it has noting to do with the database until you attach it to the database.
There are 2 things you can do to be able to remove your item:
db.Set<T>.Attach(item);
// now you should be able to remove it
or
item = db.Set<T>.Single(x => x.Id == itme.Id);
// now you should be able to remove it
EDIT
You should also make sure that you're not trying to attach an object that already is attached. Take a look at the EntitState:
db.Entry(item).State

How can I use DbContext in C# instead of Entity Framework [duplicate]

This question already has answers here:
Fake DbContext of Entity Framework 4.1 to Test
(5 answers)
How to mock DbContext [duplicate]
(1 answer)
Closed 8 years ago.
I have some questions:
How can I use DbContext in C# instead of Entity Framework?
How can I mock DbContext in C#?
Please give me solution?
Testing with a mocking framework (EF6 onwards)
Testing with your own test doubles

How can I remove all records from a table? [duplicate]

This question already has answers here:
Entity Framework. Delete all rows in table
(25 answers)
Closed 9 years ago.
I want to delete all records from my SQL Server table. I use LINQ to EF5. Is there anything I can do to do that?
context.Entities.DeleteAllOnSubmit(dc.Entities);

How can I check the existence of database [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to check for the existence of a DB?
How can I check the existance of database and if it exist how can I delete it?
Check out this page:
http://akrabat.com/winphp-challenge/retriving-a-list-of-database-from-sql-server/
It tells you how to retrieve a list of databases on an sql-server. After that, I think a
DROP DATABASE ...
should do the trick.

Categories