Base solution for your next web application
Open Closed

How to create composite index unique in my class? #818


User avatar
0
motaj1982 created

Hello! Could you help me?

I want an entity (table) with an integer primary key (Default like shown below), but I need the fields "GenericoId" and "VersaoId" like composite index unique.

How can I do that?

public class VersaoDado : FullAuditedEntity
    {
        public virtual Guid GenericoId { get; set; }

        [ForeignKey("VersaoId")]
        public virtual int VersaoId { get; set; }
        public virtual Versao Versoes { get; set; }

    }

1 Answer(s)
  • User Avatar
    0
    guillaumemorin created

    Look at entity framework Index attribute.

    public class VersaoDado : FullAuditedEntity
        {
            [Index("IX_GenericoId_VersaoId", 1, IsUnique = true)]
            public virtual Guid GenericoId { get; set; }
    
            [Index("IX_GenericoId_VersaoId", 2, IsUnique = true)]
            [ForeignKey("VersaoId")]
            public virtual int VersaoId { get; set; }
            public virtual Versao Versoes { get; set; }
    
        }