我正在使用实体框架5(代码优先方法),并希望我的实体存储在我的数据库中(我的DbContext
中暴露了DbSet<>
属性)有一个不存储在数据库中的相关对象。实体框架中的非数据库支持的相关对象
下面是想什么,我实现一个例子:
// Stored in DB
public class RecordedMeasure
{
[Required]
public int RecordedMeasureID { get; set; }
[Required]
[StringLength(150)]
public string MeasureName { get; set; }
// Navigation Property...
// Go find the Measure by the MeasureName (which IS persisted to the DB)
public Measure Measure { get; set;}
// Additional properties removed for brevity....
}
// NOT Stored in DB
public class Measure{
public string MeasureName { get; set;}
// Additional properties removed for brevity....
}
// Allows registration and holds a static collection of all "Measures"
// that are known at runtime
public class MeasureRegistration{
// Allow access to all measures stored
public static List<Measure> Measures { get; set; }
// Register a measure
public static void Register(Measure measure) {
// Code removed for brevity...
}
}
有没有办法来揭露我MeasureRegistration.Measures
收集的东西实体框架可以像它会一个典型的DbSet工作?
谢谢!
更新/进一步明确
我知道如何防止财产被映射到然而数据库,我不知道怎么去EF实际上有一个“资源库”或集合(如DbSet<>
)可用于实现DB支持模型和非DB支持模型之间关系的非DB-BACKED模型。
你究竟是什么意思“履行关系” – cheedep
沿着'modelContext.RecordedMeasures.FirstOrDefault()。测量'的行并有EF了解如何实现我的'RecordedMeasure'的'Measure'属性。我意识到这不会是“自动”,我需要在我的设计中建立逻辑,使我可以根据自己的逻辑查询和加入“度量”的集合。 EF通过诸如'this.HasProperty(o => o.Measure).WithMany(o => o.Measures).HasForiegnKey(o => o.MeasureName)之类的映射来做到这一点;'然而......这种类型的映射会试图击中数据库...'Measure's不存储在那里 –
基本上,我想有一个不同的商店支持'Measure's(我的应用程序层是通过Collection或IQueryable的商店或任何会是必要的)以允许通过EF使用它来解决我的关系。这甚至可能不是可用的功能。 –