所以,我有几个存储在我的数据库中的类,它们上面有东西,这里是一个类的小样本:
public class Order {
//Stuff
[ForeignKey("QuoteIdField")]
public virtual Quote Quote { get; set; }
[Column("QuoteId")]
public virtual Guid? QuoteIdField { get; set; }
[NotMapped]
public Guid QuoteId {
get { return QuoteIdField.HasValue ? QuoteIdField.Value : Guid.Empty; }
set { QuoteIdField = value; }
}
//More stuff...
}
当我编写这个快速单元测试方法来检查我的实体框架集成时,我发现引用ID没有被设置。
using (var db = new DataContext()) {
var order = new Order();
//order.Quote is null
//order.QuoteIdField is null
order.Quote = db.Quotes.FirstOrDefault();
//order.Quote is not null
//order.QuoteIdField is null
db.Orders.Add(order);
}
我误解了EF的一些事情吗?分配属性时是否设置了外键?