Prroblem with ObjectDelete() in Entity Framework 4

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-05-26T04:58:33Z Indexed on 2010/05/26 5:01 UTC
Read the original article Hit count: 242

Filed under:
|
|
|

I got two entities:

public class User : Entity
{
    public virtual string Username { get; set; }
    public virtual string Email { get; set; }
    public virtual string PasswordHash { get; set; }

    public virtual List<Photo> Photos { get; set; }
}

and

public class Photo : Entity
{
    public virtual string FileName { get; set; }
    public virtual string Thumbnail { get; set; }
    public virtual string Description { get; set; }

    public virtual int UserId { get; set; }
    public virtual User User { get; set; }
}

When I try to delete the photo it works fine for the DB (record gets romoved from the table) but it doesnt for the Photos collection in the User entity (I can still see that photo in user.Photos).

This is my code. What I'm doing wrong here? Changing entity properties works fine. When I change photo FileName for example it gets updated in DB and in user.Photos.

        var photo = SelectById(id);
        Context.DeleteObject(photo);
        Context.SaveChanges();

        var user = GetUser(userName);

        // the photo I have just deleted is still in user.Photos

also tried this but getting same results:

      var photo = user.Photos.First();
      Context.DeleteObject(photo);
      Context.SaveChanges();

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET