我正在使用實體框架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使用它來解決我的關係。這甚至可能不是可用的功能。 –