我正在使用实体框架 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<>
它可以用来实现的 NON DB-BACKED 模型的集合(如一个)数据库支持的模型和非数据库支持的模型之间的关系。