问题
When Initializer.Seed()
uses DbContext.DbSet.Find()
to set the navigation properties for a new Proxy, it correctly assigns the FK's to the Proxy, but the first navigation property is always null when I break on SaveChanges()
and inspect the Proxy. Does anyone know why it does this?
(Sorry, I can't post a screenshot of the locals window.)
Model
public class Thing : Base {
public virtual Nullable<int> Option1ID { get; set; }
public virtual Option1 Option1 { get; set; }
public virtual Nullable<int> Option2ID { get; set; }
public virtual Option2 Option2 { get; set; }
public virtual Nullable<int> Option3ID { get; set; }
public virtual Option3 Option3 { get; set; }
}
public class Option1 : Base {
public virtual ICollection<Thing> Things { get; set; }
}
public class Option2 : Base {
public virtual ICollection<Thing> Things { get; set; }
}
public class Option3 : Base {
public virtual ICollection<Thing> Things { get; set; }
}
public class Base {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
DbContext
public class Context : DbContext {
public DbSet<Thing> Things { get; set; }
public DbSet<Option1> Option1s { get; set; }
public DbSet<Option2> Option2s { get; set; }
public DbSet<Option3> Option3s { get; set; }
public ObjectContext ObjectContext {
get { return ((IObjectContextAdapter)this).ObjectContext; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
}
}
Seed()
var option1s = new List<Option1> {
new Option1{Name="Red"},
new Option1{Name="Green"}};
option1s.ForEach(x => db.Option1s.Add(x));
db.SaveChanges();
var option2s = new List<Option2> {
new Option2{Name = "Tall"},
new Option2{Name = "Short"}};
option2s.ForEach(x => db.Option2s.Add(x));
db.SaveChanges();
var option3s = new List<Option3> {
new Option3{Name = "Male"},
new Option3{Name = "Female"}};
option3s.ForEach(x => db.Option3s.Add(x));
db.SaveChanges();
var thing = db.Things.Create();
thing.Option1 = db.Option1s.Find(1); //the first thing.XXXX shows null no matter what order
thing.Option2 = db.Option2s.Find(1); //but the FK's work for all three of them
thing.Option3 = db.Option3s.Find(1);
db.Things.Add(thing);
db.SaveChanges();
回答1:
Wow. I don't know what you did there, but you made the object state tracker very angry.
EDIT:
I believe this to be some sort of bug in the debugger and the way it works with dynamic proxies. I say this because you can do Console.WriteLine(thing.Option1.Id);
and it will give you the correct id. If you get rid of the dynamic proxy by saying var thing = new Thing();
, the debugger problem goes away.
Here is the more standard way to do what you're trying to do.
var option1s = new List<Option1> {
new Option1{Name="Red"},
new Option1{Name="Green"}};
option1s.ForEach(x => db.Option1s.Add(x));
var option2s = new List<Option2> {
new Option2{Name = "Tall"},
new Option2{Name = "Short"}};
option2s.ForEach(x => db.Option2s.Add(x));
var option3s = new List<Option3> {
new Option3{Name = "Male"},
new Option3{Name = "Female"}};
option3s.ForEach(x => db.Option3s.Add(x));
var thing = db.Things.Create();
thing.Option1 = option1s[0];
thing.Option2 = option2s[0];
thing.Option3 = option3s[0];
db.Things.Add(thing);
db.SaveChanges();
来源:https://stackoverflow.com/questions/6783484/proxys-first-navigation-property-is-null-when-seeding-ef-4-1