当种子EF 4.1时,代理的第一个导航属性为null [英] Proxy's First Navigation Property is null when Seeding EF 4.1
问题描述
当 Initializer.Seed()
使用 DbContext.DbSet.Find()
设置导航属性新代理,它正确地将FK分配给代理,但是当我打开 SaveChanges()
并检查时,第一个导航属性始终为空代理。有没有人知道为什么这样做?
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.)
模型
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
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);
}
}
种子()
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();
推荐答案
哇。我不知道你在那里做了什么,但是你使对象状态跟踪器很生气。
Wow. I don't know what you did there, but you made the object state tracker very angry.
编辑:
我相信这是调试器中的某种错误以及它与动态代理的工作方式。我说这个,因为你可以做 Console.WriteLine(thing.Option1.Id);
,它会给你正确的id。如果您通过说 var thing = new Thing();
来摆脱动态代理,调试器问题就会消失。
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();
这篇关于当种子EF 4.1时,代理的第一个导航属性为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!