Delete object from list database Entity
<p>I have a problem with deleting an objet from a list in a object in a database.</p>

<p>Here is my class : </p>

<pre><code>public class Client
{
    public int Id {get; set;}
    private List&lt;Project&gt; projects;
    public virtual List&lt;Projet&gt; Projects
    {
        get { return projects; }
        set
        {
            if (value != null)
                projects = value;
        }
    }
}
</code></pre>

<p>Here is the context : </p>

<pre><code>public class MyDbContext : DbContext
{
    public DbSet&lt;Client&gt; Clients {get; set;}
    public DbSet&lt;Projet&gt; Projects {get; set;}
}
</code></pre>

<p>But when in my dal, I try to do this : </p>

<pre><code>public void ClientDeleteProject(Client c, Project p)
{
    c.Projects.Remove(p);
    db.SaveChanges();
}
</code></pre>

<p>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 ...</p>

<p>What am I missing ? Whether I work with the object or its Id, the "projects" list af Client does not change ...</p>

<p>Thank you for your help !</p>

<p>Edit : </p>

<p>Here is some more concrete code : </p>

<pre><code>public class Client
{
    [Key]
    public int Id { get; set; }
    private string name;
    private string address;
    private string image;
    private List&lt;Contact&gt; contactsClient;
    private List&lt;Project&gt; projectsClient;
    //properties are defined after that, with the keyword "virtual" for the lists
}
</code></pre>

<p>Actually my problem is for another class but it way easier to present with "Project", here is the class with the problem : </p>

<pre><code>public class Contact : Person
{
    private Client associatedClient;
    private List&lt;Projet&gt; i;
    private List&lt;Projet&gt; ir;
    //same for the properties
}
</code></pre>

<p>Person is just a class with multiple string such as first name, last name, ... with the associated properties.</p>

<p>I tried this but it does not work. The contact is still in the database ...</p>

<pre><code>Contact c = db.Projects.FirstOrDefault(co =&gt; co.Id == id);
if( c != null) {
    db.Contacts.Remove(c);
    db.SaveChanges();
}
</code></pre>

<p>I hope that helps.</p>

<p>Could it come from the fact my classes are not POCO ?</p>