Reputation: 162
I have a problem with deleting an objet from a list in a object in a database.
Here is my class :
public class Client
{
public int Id {get; set;}
private List<Project> projects;
public virtual List<Projet> Projects
{
get { return projects; }
set
{
if (value != null)
projects = value;
}
}
}
Here is the context :
public class MyDbContext : DbContext
{
public DbSet<Client> Clients {get; set;}
public DbSet<Projet> Projects {get; set;}
}
But when in my dal, I try to do this :
public void ClientDeleteProject(Client c, Project p)
{
c.Projects.Remove(p);
db.SaveChanges();
}
Well, the list still contains the project p. I even tried to do a RemoveAll(pr => pr.Id == p.Id), also I tried to directly delee the project in the database with db.Projects.Remove(p) but still, nothing happens. I tried also to empty the list by doing a new List() by nothing happens too ...
What am I missing ? Whether I work with the object or its Id, the "projects" list af Client does not change ...
Thank you for your help !
Edit :
Here is some more concrete code :
public class Client
{
[Key]
public int Id { get; set; }
private string name;
private string address;
private string image;
private List<Contact> contactsClient;
private List<Project> projectsClient;
//properties are defined after that, with the keyword "virtual" for the lists
}
Actually my problem is for another class but it way easier to present with "Project", here is the class with the problem :
public class Contact : Person
{
private Client associatedClient;
private List<Projet> i;
private List<Projet> ir;
//same for the properties
}
Person is just a class with multiple string such as first name, last name, ... with the associated properties.
I tried this but it does not work. The contact is still in the database ...
Contact c = db.Projects.FirstOrDefault(co => co.Id == id);
if( c != null) {
db.Contacts.Remove(c);
db.SaveChanges();
}
I hope that helps.
Could it come from the fact my classes are not POCO ?
Upvotes: 0
Views: 2304
Reputation: 21
Find out the object first by its id then after delete that object
if( db.Projects.Find(id) != null) {
db.Projects.Remove(project);
db.SaveChanges();
}
Upvotes: 0
Reputation: 4544
You're not deleting anything from database.
You should remove from context in order to persist your changes Assuming that the p you're passing is a valid instance retrieve from Db with EF
db.Projects.Remove(p);
db.SaveChanges();
if not, you'll have to do this
var project = db.Project.SingleOrDefault(m => m.Id == p.Id);
if(project != null) {
db.Projects.Remove(project);
db.SaveChanges();
}
Upvotes: 0
Reputation: 2108
You are doing it wrong. To delete data you have to use the DbContext object, this is the real connector between your data and the database.
The correct way is like this:
var project = db.Projects.FirstOrDefault(p => p.Id == id);
if( project != null) {
db.Projects.Remove(project);
db.SaveChanges();
}
The other object is not related to anything. I use "FirstOrdefault" with match by Id because I don't know if your object (the project) is attached to the context.
Upvotes: 1