ef核心拥有的类型以及带有过滤器的索引

时间:2019-10-13 09:27:42

标签: .net-core entity-framework-core

我具有以下实体配置:

public class AgentConfiguration : IEntityTypeConfiguration<AgentEntity>
{
    public void Configure(EntityTypeBuilder<AgentEntity> builder)
    {
        builder.Property(x=> x.AgentId)
            .ValueGeneratedOnAdd();
        builder.HasKey(x => x.AgentId);

        builder.HasDiscriminator(x => x.ObjectType)
            .HasValue<AgentEntity>(EntityObjectType.Agent)
            .HasValue<GroupEntity>(EntityObjectType.Group);

        builder.Property(e => e.Name)
            .HasMaxLength(100);

        builder.Property(e => e.Mbox)
            .HasMaxLength(128)
            .HasColumnName("Mbox");

        builder.Property(e => e.Mbox_SHA1SUM)
            .HasMaxLength(40)
            .HasColumnName("Mbox_SHA1SUM");

        builder.Property(e => e.OpenId)
           .HasColumnName("OpenId");

        builder.OwnsOne(x => x.Account, accountBuilder => { 
            accountBuilder.Property(e => e.HomePage)
                .HasColumnName("Account_HomePage")
                .HasMaxLength(Constants.MAX_URL_LENGTH);

            accountBuilder.Property(e => e.Name)
                .HasColumnName("Account_Name")
                .HasMaxLength(40);
        });

        builder
            .HasIndex(x => new { x.ObjectType, x.Mbox })
            .HasFilter("[Mbox] IS NOT NULL")
            .IsUnique();

        builder
            .HasIndex(x => new { x.ObjectType, x.Mbox_SHA1SUM })
            .HasFilter("[Mbox_SHA1SUM] IS NOT NULL")
            .IsUnique();

        builder
            .HasIndex(x => new { x.ObjectType, x.OpenId })
            .HasFilter("[OpenId] IS NOT NULL")
            .IsUnique();

        builder
            .HasIndex("ObjectType", "Account_Name", "Account_HomePage")
            .HasFilter("[Account_Name] IS NOT NULL AND [Account_HomePage] IS NOT NULL")
            .IsUnique();
    }
}

我很难配置的行如下,该行应将过滤器应用于拥有的类型:帐户

     builder.HasIndex("ObjectType", "Account.Name", "Account.HomePage")
            .HasFilter("[Account_Name] IS NOT NULL AND [Account_HomePage] IS NOT NULL")
            .IsUnique() 

此行不起作用,因为父实体上没有名称为Account.Name或Account.HomePage的属性

我也尝试了以下方法:

     builder.HasIndex(new { x.ObjectType, x.Account.Name, x.Account.HomePage})
            .HasFilter("[Account_Name] IS NOT NULL AND [Account_HomePage] IS NOT NULL")
            .IsUnique() 

但这也不起作用,我得到以下输出:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.InvalidOperationException: The property 'Account.Name' cannot be added to the type 'AgentEntity' because there was no property type specified and there is no corresponding CLR property or field. To add a shadow state property the property type must be specified.

我可能最终得到一个配置,其中Account驻留在新表中。但是我想做以下工作。 任何帮助深表感谢!

0 个答案:

没有答案