Question

I think i ran into a bug, it seems that EF is not handling references well after deleting and reinserting an entity. I've managed to reproduce it with the code below (assume all asserts pass except the one i talk about in the comments):

var database = new TestEntities();

// select and delete the info record
var info = database.Info.First(i => i.ID == 1);
Assert.AreEqual(1, info.MemberID);
// when i uncomment the line below the last Assert fails
// Assert.IsNotNull(info.Member);
database.Info.Remove(info);

// add it again and persist it to the database
database.Info.Add(new Info {
    ID = 1,
    MemberID = 1
});
database.SaveChanges();

// should not be null ? EDIT: i guess i understand this becoming null
Assert.IsNull(info.Member);

// and even here its still null
info = database.Info.First(i => i.ID == 1);
Assert.IsNull(info.Member);


Can anyone tell me whats going on here?

EDIT: My entities are generated using database first and im using the DbContext/POCO generator.

public partial class Member
{
    public Member()
    {
        this.Info = new HashSet<Info>();
    }

    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Info> Info { get; set; }
}

public partial class Info
{
    public int ID { get; set; }
    public int MemberID { get; set; }

    public virtual Member Member { get; set; }
}
Was it helpful?

Solution

It turns out that it had nothing to do with deleting and reinserting, not really anyway. It was was so obvious ...

I was inserting using a POCO which is not eagerly loaded and does not have any lazy loading capabilities ...

The second time i queried for the same record i was expecting a proxy, but it seems that the POCO was cached by EF and that is what it returned meaning still no eager or lazy loading.

I can fix it by making sure EF doesn't retrieve the second query from cache, inserting using a proxy (var info = database.Info.Create()) or including member in the query (database.Info.Include(i => i.Member).First(i => i == 1)).

OTHER TIPS

Given this

var info = database.Info.First(i => i.ID == 1);
Assert.AreEqual(1, info.MemberID);

Aren't you comparing info.ID to info.MemberID here? Isn't it possible that ID and MemberID actually are different?

Also, shouldn't you be using .SaveChanges() after

database.Info.Remove(info);

?

Also, info does not have .member available if none have been instantiated. Is there a correlating Member with MemberID equal to info.MemberId?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top