1
我有以下兩種模式EF4.1模型複雜類型的主鍵字符串都不盡如人意
[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是我剛添加到db的對象,pl也不是null。
但是,當我嘗試從控制器其中5是新的事件對象
var ev = _db.Events.Find(5);
var evPlace = ev.Place;
var pl = _db.Places.Find("{59E0F90C-03DB-453C-8C9E-4747957ED37E}");
PL和EV不爲空的ID運行下面的代碼但ev.Place爲空(evPlace)
任何想法爲什麼?