CannotDelete Entity in Database [duplicate] - c#

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

Related

Getting a NullReferenceException while script is functioning and following traditional debugging methods aren't effective [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm trying to do a little bit of cleaning up and I seem to be getting the follow
NullReferenceException: Object reference not set to an instance of an
object DestroyCollision.OnCollisionEnter2D (UnityEngine.Collision2D
collision) (at Assets/Scripts/DestroyCollision.cs:66)
Now though, even though the entire script works perfectly (there's literally no bugs or anything) and the object reference is set correctly (since the object in question gets its active state set to false), I am stumped as to why there are any issues. The following code is the line in question:
if (collision.gameObject.tag == "Shield")
{
GameObject.FindGameObjectWithTag("Shield").SetActive(false);
Destroy(this.gameObject);
}
The shield object is properly tagged, and as I said, setactive(false) gets applied. Line 66 is the gameobject.find... of that line of code there.
Why am I getting the error and how to fix it?
Are you sure you mean to have Destroy(this.gameObject); instead of Destroy(collision.gameObject);?
We know collision.gameObject is not null there, but you provide no info on what this.gameObject is.

"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);

EntityFramework - Cannot Delete Entity using DbSet [duplicate]

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();

Application.OpenForms - seems to be missing [duplicate]

This question already has answers here:
WPF version of Application.OpenForms
(3 answers)
Closed 8 years ago.
I have a small wpf/c# application and want to get a list of all of the open views. However, the Application.OpenForms, does not have "OpenForms" in the intellisense (and gives me an error). Could I be missing a specific reference of something?
I have tried: System.Windows.Application.OpenForms;
Based on the docs for the Application class, there is no OpenForms member.
http://msdn.microsoft.com/en-us/library/system.windows.application(v=vs.110).aspx
Try Application.Windows
var openWindows = System.Windows.Application.Current.Windows;

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