不久,我想在保留主键的表上创建复合键,以提高 sql server 搜索性能。每当我搜索没有主键的实体(即 GUID 字符串)时,性能问题就会出现在 200k 数据表上。假设我有 3 节课
public class Device{
public int ID { get; set; }
public string UDID { get; set; }
public string ApplicationKey { get; set; }
public string PlatformKey { get; set; }
public ICollection<NotificationMessageDevice> DeviceMessages { get; set; }
}
public class NotificationMessageDevice {
[Column(Order = 0), Key, ForeignKey("NotificationMessage")]
public int NotificationMessage_ID { get; set; }
[Column(Order = 1), Key, ForeignKey("Device")]
public int Device_ID { get; set; }
public virtual Device Device { get; set; }
public virtual NotificationMessage NotificationMessage { get; set; }
}
public class NotificationMessage {
public int ID { get; set; }
public string Text { get; set; }
public DateTime CreateDate { get; set; }
}
modelBuilder.Entity<Device>().HasKey(t => new { t.ID, t.ApplicationKey, t.PlatformKey, t.UDID });
问题是,每当我想使用 modelBuilder 将 ID 、 UDID 、 ApplicationKey 和 PlatformKey 定义为复合键时,都会出现以下错误。
NotificationMessageDevice_Device_Target_NotificationMessageDevice_Device_Source: : 关系约束中的从属角色和主体角色中的属性数量必须相同
我认为问题是因为 NotificationMessageDevice 上的导航属性无法识别 Device 表上的主键。我该如何解决这个问题?除此之外,如果您分享您提高实体框架搜索性能的经验,我将很高兴。通常,每当我使用没有主键的第一种方法时,都会出现性能问题。