我必须将以下实体存储在数据库中:
其中每个设备都有一个或多个测量点。测量值表将每10分钟存储一次每个测量点的值。这张表在几年后将有数以百万计的记录,必须通过测量点和时间戳有效地搜索。您将如何设计这与EF6代码-优先?
我的第一个方法是:
public class Device
{
public int ID { get; set; }
public int DeviceNumber { get; set; }
...
public virtual ICollection<MeasuringPoint> MeasuringPoints { get; set; }
}
public class MeasuringPoint
{
public int ID { get; set; }
public int MeasuringPointNumber { get; set; }
...
// Foreign key
public int DeviceID { get; set; }
public virtual Device Device { get; set; }
}
public class MeasuredValue
{
//public int Id { get; set; } ????
public DateTime TimeStamp { get; set; }
// Foreign key
public int MeasuringPointID { get; set; }
public double Value;
public virtual MeasuringPoint MeasuringPoint { get; set; }
}在TimeStamp和MeasuringPointID组合中,测量值表中的所有值必须是唯一的。我应该为MeasuredTable定义什么主键?
发布于 2015-02-11 14:30:47
这并不是唯一与code_first/EF相关的。
使用EF,您可以选择将导致创建集群索引的PK。然后,您可能/必须为querie创建特定的索引(Es)。
Imho,你可以:
(max for point) + 1的东西,甚至是日期(但是如果在同一毫秒内有2个度量呢?)
根据您的事务和并发压力,max解决方案可能很难实现。
在最后一种情况下,您将拥有如下的配置:
public class MeasuredValueConfiguration : EntityTypeConfiguration<MeasuredValue>{
public MeasuredValueConfiguration()
: base() {
/* ... */
HasKey(e => new {e.MeasuringPointID, e.Id});
/* ... */
}
}https://stackoverflow.com/questions/28456183
复制相似问题