[英]Entity Framework - Code First mapping issue
是否有可能基於特定的列值進行外鍵映射。
我有以下實體。
public class Controller
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<ControllerDevice> ActiveDevices { get; set; }
public virtual List<ControllerDevice> TamperedDevices { get; set; }
public virtual List<ControllerDevice> IgnoredDevices { get; set; }
}
public class ControllerDevice
{
public int Id { get; set; }
public DeviceStatus Status { get; set; }
public int ControllerId { get; set; }
public int NetworkDeviceId { get; set; }
public virtual Controller Controller { get; set; }
public virtual NetowkDevice NetowkDevice { get; set; }
}
public class NetowkDevice
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public enum DeviceStatus
{
Active,
Tampered,
Ignored
}
是否有可能有ActiveDevices
, TamperedDevices
和IngoredDevices
列表進行自動填充基於ControllerDevice
DeviceStatus
,否則我將不得不為每個列表三個不同的表。 IE ActiveControllerDevice
, TamperedControllerDevices
和IgnoredControllerDevices
。
如果您需要進一步的說明,請告訴我。
使用單個設備集合:
public class Controller
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<ControllerDevice> Devices { get; set; }
}
...並過濾它,當您需要處理或顯示具有特定Status
值的設備時:
controller.Devices.Where(d => d.Status == DeviceStatus.Active);
幾個表對每個設備的狀態,和/或設備層次(從理論上說,你可以解決這個問題了TPH繼承)是通向地獄之路,因為不是單一實體ControllerDevice
一個狀態,你會得到三個實體類型( ActiveControllerDevice
, TamperedControllerDevice
和IgnoredControllerDevice
),這是不對應於建模。
設備不會更改狀態,而是會更改其類型 ,而您不能通過簡單的方式來更改它。
是的,你可以這么做。 實體框架5,.Net Framework 4.5中引入了枚舉支持。 在Entity Framework中,枚舉可以具有以下基礎類型:Byte,Int16,Int32,Int64或SByte。
您可以這樣過濾:
context.ControllerDevices.Where(d => d.Status == DeviceStatus.Active);
此處更多信息: http : //msdn.microsoft.com/zh-cn/data/hh859576.aspx
public class TestContext : DbContext
{
public TestContext()
{
Configuration.AutoDetectChangesEnabled = true;
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
public virtual DbSet<NetowkDevice> NetowkDevices{ get; set; }
public virtual DbSet<ControllerDevice> ControllerDevices{ get; set; }
public virtual DbSet<Controller> Controlleres{ get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.