使用HasColumnAnnotation可以实现多个索引?
它看起来像在Entity Framework 6.1中,它们添加了通过新的HasColumnAnnotation
方法创建表索引的HasColumnAnnotation
。 我创建了一些辅助扩展来加速这个过程:
public static class MappingExtensions { public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, bool isUnique = false) { return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = isUnique })); } public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, string name, int order = 1, bool isUnique = false) { return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute(name, order) { IsUnique = isUnique })); } public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, bool isUnique = false) { return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = isUnique })); } public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, string name, int order = 1, bool isUnique = false) { return config.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute(name, order) { IsUnique = isUnique })); } }
这很棒…直到我尝试创建第二个索引,其中包含已在另一个索引中使用的列。 无论我添加什么,最后覆盖原始。 有没有人知道目前是否可以通过StringPropertyConfiguration
和PrimitivePropertyConfiguration
上提供的新HasColumnAnnotation
将多个索引添加到同一列?
我可以通过在迁移脚本中手动添加索引来解决这个问题,但是最好能够在EntityTypeConfiguration
映射中配置它,这样我就可以在一个位置完成所有操作。
在Gerts反馈之后,这就是我最终做的事情:
public static class MappingExtensions { public static StringPropertyConfiguration HasIndex(this StringPropertyConfiguration config, params IndexAttribute[] indexes) { return config.HasColumnAnnotation("Index", new IndexAnnotation(indexes)); } public static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration config, params IndexAttribute[] indexes) { return config.HasColumnAnnotation("Index", new IndexAnnotation(indexes)); } }
以下是新用法:
Property(x => x.Name).IsRequired().HasMaxLength(65).HasIndex(new IndexAttribute("IX_Countries_Name") { IsUnique = true }, new IndexAttribute("IX_Countries_Published", 2))
这是因为您的每个扩展方法都会为属性分配一个新注释并覆盖前一个注释。 让我通过在示例中使用您的方法来表明。
假设我们有这个(无用的)课程
public class Client { public int ClientId { get; set; } public int CompanyId { get; set; } public int AddressId { get; set; } }
并应用您的索引定义(跳过部分modelBuilder.Entity
):
.Property(c => c.ClientId).HasIndex("ClientCompanyIndex"); .Property(c => c.CompanyId).HasIndex("ClientCompanyIndex", 2); .Property(c => c.ClientId).HasIndex("ClientAddressIndex"); .Property(c => c.AddressId).HasIndex("ClientAddressIndex", 2);
内联扩展方法(感谢God for Resharper)这导致了
.Property(c => c.ClientId).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ClientCompanyIndex", 1)); .Property(c => c.CompanyId).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ClientCompanyIndex", 2)); .Property(c => c.ClientId).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ClientAddressIndex", 1)); .Property(c => c.AddressId).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("ClientAddressIndex", 2));
这和写作一样
[Index("ClientCompanyIndex", Order = 1)] public int ClientId { get; set; }
然后替换它
[Index("ClientAddressIndex", Order = 1)] public int ClientId { get; set; }
要重现正确的注释……
[Index("ClientAddressIndex", IsUnique = true, Order = 1)] [Index("ClientCompanyIndex", IsUnique = true, Order = 1)] public int ClientId { get; set; } [Index("ClientCompanyIndex", IsUnique = true, Order = 2)] public int CompanyId { get; set; } [Index("ClientAddressIndex", IsUnique = true, Order = 2)] public int AddressId { get; set; }
… ClientId
属性的配置应如下所示
.Property(c => c.ClientId).HasColumnAnnotation("Index", new IndexAnnotation(new[] { new IndexAttribute("ClientCompanyIndex", 1), new IndexAttribute("ClientAddressIndex", 1) }));
现在突然创建扩展方法的吸引力要小得多。 为这个组合注释创建一个是不值得的。 但对于一次性色谱柱,您的方法是一种改进。
当然很清楚你为什么要这样做。 目前流畅的语法至少可以说是笨拙的。 EF团队非常了解这一点 ,他们希望有一些贡献者尽快解决这个问题。 也许适合你的东西?
上述就是C#学习教程:使用HasColumnAnnotation可以实现多个索引?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/984762.html