我有以下两种型号
[DataContract]
public class Event
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public Place Place { get; set; }
}
[DataContract]
public class Place
{
[Key]
public string ID { get; set; }
public string Name { get; set; }
}
和数据上下文类:
public class myDB: DbContext
{
public DbSet<Event> Events { get; set; }
public DbSet<Place> Places { get; set; }
}
例如,我运行以下代码
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Foo();
}
private void Foo()
{
var db = new myDB();
var place = new Place
{
ID = "{59E0F90C-03DB-453C-8C9E-4747957ED37E}",
Name = "Home",
};
db.Places.Add(place);
db.SaveChanges();
var @event = new Event
{
Name = "Test",
Place = place,
TimeFrame = new TimeFrame() {StartTime = DateTime.Now, EndTime = DateTime.Now }
};
db.Events.Add(@event);
db.SaveChanges();
var eventIndex = @event.ID;
var ev = db.Events.Find(@eventIndex);
var evPlace = ev.Place;
var pl = db.Places.Find("{59E0F90C-03DB-453C-8C9E-4747957ED37E}");
}
一切正常,evPlace 是我刚刚添加到数据库的对象,pl 也不为空。
但是当我尝试从控制器运行以下代码时,其中 5 是新事件对象的 ID
var ev = _db.Events.Find(5);
var evPlace = ev.Place;
var pl = _db.Places.Find("{59E0F90C-03DB-453C-8C9E-4747957ED37E}");
pl 和 ev 不为空,但 ev.Place 为空 (evPlace)
知道为什么吗?