不久,我想在我的表上创建组合键与主键,以提高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 });
的问题是什么,每当我想要的ID,UDID,ApplicationKey和PlatformKey定义为复合键与模型构建器它提供了以下错误。
NotificationMessageDevice_Device_Target_NotificationMessageDevice_Device_Source: :在一个 关系约束的依赖和主要角色的属性数必须相同
我认为这个问题是因为NotificationMessageDevice导航性能是无法识别Device表上的主键是什么。我该如何解决这个问题?除此之外,如果您分享改善Entity框架搜索性能的经验,我会很高兴。通常,无论何时使用没有主键的First方法,性能问题都会发生。
我对EF开发并没有太多的想法,但是你没有'ForeignKeyAttribute'对错误的东西? – 2013-02-14 10:55:46
Nop这是正确:) – kkocabiyik 2013-02-14 10:58:05