多个索引可以使用HasColumnAnnotation? [英] Multiple indexes possible using HasColumnAnnotation?

查看:758
本文介绍了多个索引可以使用HasColumnAnnotation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像在实体框架6.1增加他们通过新的 HasColumnAnnotation 方法来创建表索引的能力。我创建了几个帮手扩展进程加快:

 公共静态类MappingExtensions 
{
公静态StringPropertyConfiguration HasIndex(这StringPropertyConfiguration配置,isUnique设置布尔= FALSE)
{
返回config.HasColumnAnnotation(指数,新IndexAnnotation(新IndexAttribute(){= isUnique设置isUnique设置}));
}
公共静态StringPropertyConfiguration HasIndex(这StringPropertyConfiguration配置,字符串名称,诠释秩序= 1,isUnique设置布尔= FALSE)
{
返回config.HasColumnAnnotation(指数,新IndexAnnotation(新IndexAttribute(名称,订单){= isUnique设置isUnique设置}));
}
公共静态PrimitivePropertyConfiguration HasIndex(这PrimitivePropertyConfiguration配置,isUnique设置布尔= FALSE)
{
返回config.HasColumnAnnotation(指数,新IndexAnnotation(新IndexAttribute(){isUnique设置= isUnique设置}));
}
公共静态PrimitivePropertyConfiguration HasIndex(这PrimitivePropertyConfiguration配置,字符串名称,诠释秩序= 1,isUnique设置布尔= FALSE)
{
返回config.HasColumnAnnotation(指数,新IndexAnnotation(新IndexAttribute(名称,订单){= isUnique设置isUnique设置}));
}
}

这梦幻般的工作......直到我试图创建包含在另一个索引已经使用的柱的第二索引。无论我最后补充覆盖原来的。有谁知道,如果它是目前可以通过多个指标添加到同一列的新 HasColumnAnnotation 可在 StringPropertyConfiguration PrimitivePropertyConfiguration



我可以解决这个就像我总是在迁移脚本手动添加索引也有,但这将是最优秀的,能够在 EntityTypeConfiguration 映射配置这个,所以我可以拥有这一切在一个地方。






耶茨的反馈后,这是我落得这样做:

 公共静态类MappingExtensions 
{
公共静态StringPropertyConfiguration HasIndex(这StringPropertyConfiguration配置,则params IndexAttribute []索引)
{
返回config.HasColumnAnnotation(指数,新IndexAnnotation(索引));
}

公共静态PrimitivePropertyConfiguration HasIndex(这PrimitivePropertyConfiguration配置,则params IndexAttribute []索引)
{
返回config.HasColumnAnnotation(指数,新IndexAnnotation(索引));
}
}

和这里是新用法:



 属性(X => x.Name)。.IsRequired()HasMaxLength(65).HasIndex(新IndexAttribute(IX_Countries_Name){ isUnique设置= TRUE},新IndexAttribute(IX_Countries_Published,2))


解决方案

这是因为你的每一个扩展方法分配一个新的注释到属性并覆​​盖前一个。让我表明,通过使用一个例子你的方法。



假设我们有这个(没用)类

 公共类客户端
{
公众诠释客户端Id {获取;组; }
公众诠释CompanyId {搞定;组; }
公众诠释AddressId {搞定;组; }
}

和适用的索引定义(跳过部分 modelBuilder.Entity<客户>()):

  .Property(C =℃。客户端Id).HasIndex(ClientCompanyIndex); 
.Property(C => c.CompanyId).HasIndex(ClientCompanyIndex,2);
.Property(C => c.ClientId).HasIndex(ClientAddressIndex);
.Property(C => c.AddressId).HasIndex(ClientAddressIndex,2);



内联扩展方法(感谢上帝ReSharper的),这将导致



  .Property(C => c.ClientId).HasColumnAnnotation(指数,
新IndexAnnotation(新IndexAttribute(ClientCompanyIndex,1) );
.Property(C => c.CompanyId).HasColumnAnnotation(索引,
新IndexAnnotation(新IndexAttribute(ClientCompanyIndex,2));
.Property(三=> c.ClientId).HasColumnAnnotation(索引,
新IndexAnnotation(新IndexAttribute(ClientAddressIndex,1));
.Property(C => c.AddressId).HasColumnAnnotation( 索引,
新IndexAnnotation(新IndexAttribute(ClientAddressIndex,2));

这是一样的写

  [指数(ClientCompanyIndex,令= 1)] 
公众诠释客户端Id {获取;集;}

然后的替换的是由

  [指数(ClientAddressIndex,令= 1)] 
公众诠释客户端Id {获取;组; }

要重现正确标注...

  [指数(ClientAddressIndex,isUnique设置= TRUE,订单= 1)] 
[指数(ClientCompanyIndex,isUnique设置= TRUE,订单= 1)]
公众诠释客户端Id {获取;组; }
[指数(ClientCompanyIndex= isUnique设置真实,令= 2)]
公众诠释CompanyId {搞定;组; }
[指数(ClientAddressIndex= isUnique设置真实,令= 2)]
公众诠释AddressId {搞定;组; }



...的客户端Id 属性应该看起来像

  .Property(C => c.ClientId).HasColumnAnnotation(指数,
新IndexAnnotation(新[]
{
新IndexAttribute(ClientCompanyIndex,1),
新IndexAttribute(ClientAddressIndex,1)
}));

现在突然产生的扩展方法是远远低于吸引力。它几乎不值得创建一个针对该组合标注的努力。但对于一次性使用的列的方法是一种进步。



当然很清楚,为什么你想这一点。目前流利的语法是笨重的,至少可以说。 EF英孚团队知道此道以及他们希望一些贡献者很快抓住这个问题。也许东西给你?


It looks like in Entity Framework 6.1 they added the ability to create table indexes via the new HasColumnAnnotation method. I created a few helper extensions to speed up the process:

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 }));
    }
}

This works fantastic...until I try to create a second index that contains a column already used in another index. Whatever I add last overwrites the original. Does anyone know if it is currently possible to add multiple indexes to the same column via the new HasColumnAnnotation available on the StringPropertyConfiguration and PrimitivePropertyConfiguration?

I can work around this like I always have by manually adding indexes in the Migration scripts, but it would be most excellent to be able to configure this in the EntityTypeConfiguration mappings so I can have it all in one spot.


After Gerts feedback, this is what I ended up doing:

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));
    }
}

And here is the new usage:

Property(x => x.Name).IsRequired().HasMaxLength(65).HasIndex(new IndexAttribute("IX_Countries_Name") { IsUnique = true }, new IndexAttribute("IX_Countries_Published", 2))

解决方案

This is because each of your extension methods assign a new annotation to a property and overwrite the previous one. Let me show that by using your methods in an example.

Say we have this (useless) class

public class Client
{
    public int ClientId { get; set; }
    public int CompanyId { get; set; }
    public int AddressId { get; set; }
}

And apply your index definitions (skipping the part modelBuilder.Entity<Client>()):

.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);

Inlining the extension methods (thank God for Resharper) this leads to

.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));

This is the same as writing

[Index("ClientCompanyIndex", Order = 1)]
public int ClientId { get; set; }

and then replacing it by

[Index("ClientAddressIndex", Order = 1)]
public int ClientId { get; set; }

To reproduce the correct annotation...

[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; }

...the configuration of the ClientId property should look like

.Property(c => c.ClientId).HasColumnAnnotation("Index",
    new IndexAnnotation(new[]
        {
            new IndexAttribute("ClientCompanyIndex", 1),
            new IndexAttribute("ClientAddressIndex", 1)
        }));

Now suddenly creating extension methods is far less appealing. It's hardly worth the effort to create one for this combined annotation. But for single-use columns your methods are an improvement.

Of course it is clear why you're trying this. The current fluent syntax is clunky to say the least. The EF team knows this perfectly well and they're hoping for some contributor to grab this issue soon. Maybe something for you?

这篇关于多个索引可以使用HasColumnAnnotation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆