C# 实体框架-代码优先映射问题
是否可以基于特定列值进行外键映射 我有以下实体C# 实体框架-代码优先映射问题,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,是否可以基于特定列值进行外键映射 我有以下实体 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
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
}
公共类控制器
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟列表活动设备{get;set;}
公共虚拟列表篡改设备{get;set;}
公共虚拟列表IgnoredDevices{get;set;}
}
公共类控制器设备
{
公共int Id{get;set;}
公用设备状态{get;set;}
public int ControllerId{get;set;}
公共int NetworkDeviceId{get;set;}
公共虚拟控制器{get;set;}
公共虚拟NetowkDevice NetowkDevice{get;set;}
}
公共类NetowkDevice
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共枚举设备状态
{
活跃的,
篡改,
忽略
}
是否可以基于控制器设备
设备状态
自动填充活动设备
、篡改设备
和IngoredDevices
列表,或者我必须为每个列表创建三个不同的表。IEActiveControllerDevice
,TamperedControllerDevices
和IgnoredControllerDevices
如果您需要进一步解释,请告诉我。使用单个设备集合:
public class Controller
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<ControllerDevice> Devices { get; set; }
}
每个设备状态和/或设备层次结构的多个表(理论上,您可以通过TPH继承解决此问题)是一种地狱之路,因为您将获得三种实体类型,而不是具有状态的单个实体ControllerDevice
(ActiveControllerDevice
,TamperedControllerDevice
和IgnoredControllerDevice
),与型号不对应
设备不会更改状态,而是会更改其类型,而您不能简单地执行此操作。是的,您可以执行此操作。在Entity Framework 5、.Net Framework 4.5中引入了枚举支持。在Entity Framework中,枚举可以具有以下基本类型:Byte、Int16、Int32、Int64或SByte 您可以这样过滤:
context.ControllerDevices.Where(d => d.Status == DeviceStatus.Active);
更多信息:公共类TestContext:DbContext
{
公共TestContext()
{
Configuration.AutoDetectChangesEnabled=true;
Configuration.LazyLoadingEnabled=true;
Configuration.ProxyCreationEnabled=true;
Configuration.ValidateOnSaveEnabled=true;
}
公共虚拟DbSet NetowkDevices{get;set;}
公共虚拟数据库集控制器设备{get;set;}
公共虚拟数据库集控制器{get;set;}
}
OP询问映射时过滤数据的能力,而不是查询。这如何回答最初的问题?嗨,丹尼斯,我想我是想把事情复杂化,这看起来是一个更好的解决方案。谢谢。
context.ControllerDevices.Where(d => d.Status == DeviceStatus.Active);
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; }
}