How to create a composite index in SQLite via Microsoft.EntityFrameworkCore.SQLite?

2

I'm trying to create a single compound index, but I do not know how to do this in Microsoft.EntityFrameworkCore.SQLite , I'm used to working only with Doctrine and Hibernate and I'm totally lost.

This is my template class:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

namespace Test.Entity
{
    public class Project
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        private int id;
        [Required]
        private bool shared;
        [Required, MaxLength(64)]
        private String name;

        private Project parent;

        [ForeignKey("id")]
        private ICollection<Project> searchable;

        public bool Shared
        {
            get { return shared; }
            set { shared = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Project Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public ICollection<Project> Searchable
        {
            get { return searchable; }
            set { searchable = value; }
        }
    }
}

This is my method in my context class

protected override void OnModelCreating(ModelBuilder modelBuilder) {
   modelBuilder.Entity<Project>().HasIndex(r => new { r.Name, r.Parent}).IsUnique();
}
    
asked by anonymous 05.01.2017 / 16:00

1 answer

2

I use a slightly different procedure than yours but the result should be the same:

 entityBuilder.HasIndex(c => new { c.AutorNome, c.Nome})
                .HasName("idx_Curso_Autor_Nome")
                .IsUnique();

Manager result:

Iseetwopossibilitiesfornotbeingabletogenerateyourindex:

  • YouarenotrunningthemigrationAdd-MigrationIndiceComposto
  • Youareusingdataannotationalongwithfluentapi,itwouldbebesttodoeverythingwithfluentapi.Notethatin documentation you can not report composite keys with data annotations . I know you are not generating the composite index this way but it is good to standardize.
  • Responding more specifically to the question:

    I think there is no way to report a class as an index.

    How will EF know what exactly which field is it crawling?

      

    The property 'xxx' can not be added to the entity type 'xxx'

        
    06.01.2017 / 01:15