Base solution for your next web application
Open Closed

[Index] Data Annotation #2199


User avatar
0
joffies created

Good Afternoon

I have tried to use the [Index] Entity Framework Data Annotation but cannot seem to use it.

I have also added a reference to what I believe to be the correct namespace

using System.ComponentModel.DataAnnotations.Schema;

What version of the Entity Framework does the template work with? Can i decorate my Class/Entity Properties with the Index Attribute or would I have to manually create/add an Index in the migration files?

Cheers


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The Index attribute was introduced in Entity Framework 6.1, see <a class="postlink" href="https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113">https://msdn.microsoft.com/en-us/librar ... 3(v=vs.113</a>).aspx.

    Whap happens when you add a migration after adding this attribute ? You just get an empty migration class ?

    By the way, it is better to create this index in the OnModelCreating of your db context. In that way, you will not add a reference to EntityFramework library in your Core project.

    modelBuilder.Entity<User>()
                .Property(e => e.ProfilePictureId)
                .HasColumnAnnotation(
                    IndexAnnotation.AnnotationName,
                    new IndexAnnotation(new IndexAttribute())
                );
    
  • User Avatar
    0
    joffies created

    I haven't tried adding the Entity Framework to my core project yet so was confused at the time in the early hours of the morning :D .

    Following your advice by not referencing the EF in the Core project and adding the indexes using OnModelCreating in my DBContext class as follows.

    modelBuilder.Entity<Person>().Property(e => e.OrganizationUnitId)
                  .HasColumnAnnotation(
                        IndexAnnotation.AnnotationName,
                        new IndexAnnotation(new IndexAttribute("IX_ShareSource_OUId_NationalId") { IsUnique = true, Order = 1})
                  );
                modelBuilder.Entity<Person>().Property(e => e.NationalId)
                 .HasColumnAnnotation(
                       IndexAnnotation.AnnotationName,
                       new IndexAnnotation(new IndexAttribute("IX_ShareSource_OUId_NationalId") { IsUnique = true, Order =2 })
                 );
    

    I then add a migration which is where something funny is happening. The new migration has entries for allot of other tables.

    public partial class Create_Indexes_For_Person_Table : DbMigration
        {
            public override void Up()
            {
                DropIndex("dbo.AbpBackgroundJobs", new[] { "IsAbandoned", "NextTryTime" });
                DropIndex("dbo.AbpNotificationSubscriptions", new[] { "NotificationName", "EntityTypeName", "EntityId", "UserId" });
                DropIndex("dbo.SPMShareSources", new[] { "OrganizationUnitId" });
                DropIndex("dbo.AbpUserLoginAttempts", new[] { "UserId", "TenantId" });
                DropIndex("dbo.AbpUserLoginAttempts", new[] { "TenancyName", "UserNameOrEmailAddress", "Result" });
                DropIndex("dbo.AbpUserNotifications", new[] { "UserId", "State", "CreationTime" });
                AlterTableAnnotations(
                    "dbo.AbpAuditLogs",
                    c => new
                        {
                            Id = c.Long(nullable: false, identity: true),
                            TenantId = c.Int(),
                            UserId = c.Long(),
                            ServiceName = c.String(maxLength: 256),
                            MethodName = c.String(maxLength: 256),
                            Parameters = c.String(maxLength: 1024),
                            ExecutionTime = c.DateTime(nullable: false),
                            ExecutionDuration = c.Int(nullable: false),
                            ClientIpAddress = c.String(maxLength: 64),
                            ClientName = c.String(maxLength: 128),
                            BrowserInfo = c.String(maxLength: 256),
                            Exception = c.String(maxLength: 2000),
                            ImpersonatorUserId = c.Long(),
                            ImpersonatorTenantId = c.Int(),
                            CustomData = c.String(maxLength: 2000),
                        },
                    annotations: new Dictionary<string, AnnotationValues>
                    {
                        { 
                            "DynamicFilter_AuditLog_MayHaveTenant",
                            new AnnotationValues(oldValue: "EntityFramework.DynamicFilters.DynamicFilterDefinition", newValue: null)
                        },
                    });
                
                AlterTableAnnotations(
                    "dbo.AppBinaryObjects",
                    c => new
                        {
                            Id = c.Guid(nullable: false),
                            TenantId = c.Int(),
                            Bytes = c.Binary(nullable: false),
      },
                    annotations: new Dictionary<string, AnnotationValues>
                    {
                        { 
                            "DynamicFilter_BinaryObject_MayHaveTenant",
                            new AnnotationValues(oldValue: "EntityFramework.DynamicFilters.DynamicFilterDefinition", newValue: null)
                        },
                    });
                
                AlterTableAnnotations(
                    "dbo.AppChatMessages",
                    c => new
                        {
                            Id = c.Long(nullable: false, identity: true),
                            UserId = c.Long(nullable: false),
                            TenantId = c.Int(),
                            TargetUserId = c.Long(nullable: false),
                            TargetTenantId = c.Int(),
                            Message = c.String(nullable: false),
                            CreationTime = c.DateTime(nullable: false),
                            Side = c.Int(nullable: false),
                            ReadState = c.Int(nullable: false),
                        },
                    annotations: new Dictionary<string, AnnotationValues>
                    {
    

    And so on..

    I have only added those two Indexes yet other tables are now affected?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Do you call "base.OnModelCreating(modelBuilder);" when you override OnModelCreating ? It should be the first line and then you need to add your Index configurations.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        ...
        Add your code here...
        ...
    }
    
  • User Avatar
    0
    joffies created

    Thanks, that was exactly it.