C# 创建复合键实体框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14873169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Composite Key Entity Framework
提问by kkocabiyik
Shortly, I want to create composite keys on my table remaining with the primary key in order to improve sql server search performance. The performance issue occurs on 200k data table whenever I search an entity without primary key (i.e a string of GUID). Assume that I have 3 classes
不久,我想在保留主键的表上创建复合键,以提高 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 });
What the problem is that whenever I want to make ID , UDID , ApplicationKey and PlatformKey define as a Composite Key with modelBuilder it gives the following error.
问题是,每当我想使用 modelBuilder 将 ID 、 UDID 、 ApplicationKey 和 PlatformKey 定义为复合键时,都会出现以下错误。
NotificationMessageDevice_Device_Target_NotificationMessageDevice_Device_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical
NotificationMessageDevice_Device_Target_NotificationMessageDevice_Device_Source::关系约束中的从属角色和主体角色中的属性数量必须相同
I think the problem is because the navigation property on NotificationMessageDevice is not able to recognize what the primary key is on Device table. How can I resolve this problem? In addition to this I will be glad if you share your experiences improving the search performance on Entity framework. Usually the performance issue occurs on whenever I use First method without primary keys.
我认为问题是因为 NotificationMessageDevice 上的导航属性无法识别 Device 表上的主键。我该如何解决这个问题?除此之外,如果您分享您在实体框架上提高搜索性能的经验,我会很高兴。通常,每当我使用没有主键的 First 方法时,就会出现性能问题。
采纳答案by Sergey Berezovskiy
If Devicetable has composite primary key, then you need same composite foreign key on your NotificationMessageDevicetable. How would SQL find Device without full primary key? Also you should make these fields to be part of NotificationMessageDevicetable primary key. Otherwise you can't guarantee primary key will be unique:
如果Device表具有复合主键,则您的NotificationMessageDevice表上需要相同的复合外键。SQL 如何找到没有完整主键的设备?此外,您应该使这些字段成为NotificationMessageDevice表主键的一部分。否则你不能保证主键是唯一的:
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; }
[Column(Order = 2), Key, ForeignKey("Device")]
public string Device_UDID { get; set; }
[Column(Order = 3), Key, ForeignKey("Device")]
public string Device_ApplicationKey { get; set; }
public virtual Device Device { get; set; }
public virtual NotificationMessage NotificationMessage { get; set; }
}