How to create a compound index in Sqlite via Microsoft.EntityFrameworkCore.Sqlite?

Asked

Viewed 204 times

2

I’m trying to create a unique composite index, but I don’t know how to do it on Microsoft.EntityFrameworkCore.SQLite, I’m used to working only with the Doctrine and Hibernate and I’m totally lost.

This is my model 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; }
        }
    }
}

That’s my method in my context class

protected override void OnModelCreating(ModelBuilder modelBuilder) {
   modelBuilder.Entity<Project>().HasIndex(r => new { r.Name, r.Parent}).IsUnique();
}
  • A tip: Questions and Answer Formatting Link ... will help you create questions with correct layout and settings.

  • A question, you sent a code of one thing and in the configuration you are doing an index of another? would not it be better to post the doubt with the class items that make the reference of your doubt? Ta half disconnected one code from the other.

  • 1

    @Virgilionovic thanks, sorry for the error, I posted at lunch and copied the wrong excerpt I wanted to show. I already edited and corrected.

  • No need to apologize @Slienobrito normal happens, it was more a warning!

  • Is a unique index pk? A simple index? Or a composite index?

  • @rubStackOverflow It is a composite index, I made an error when posting the question and got confused.

Show 1 more comment

1 answer

2

I use a procedure little different from yours but the result should be the same:

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

Result in manager: inserir a descrição da imagem aqui

I see two possibilities for not being able to generate your index:

  1. You are not running Migration Add-Migration IndiceComposto
  2. You are using data annotation along with fluent api, would be better to do everything with fluent api. Note that in the documentation informs that it is not possible composite keys with data annotations. I know you are not generating the compound Indice this way but it is good to standardize.

Answering more specifically the question:

I think there is no possibility to inform a class as Indian.

Like the EF will know what exactly which field is wanting to index?

The Property 'xxx' cannot be Added to the Entity type 'xxx'

  • But in the question example, it would be to create a key with the Parent object that is of the Project type not with another string.

  • I understand, but I think since the bank doesn’t understand what it is objeto Parent or it may generate a key related to the index of this table, in case the ForeignKey. I’ll take another test later.

  • I managed to do, you have to create an object of the type in the case, because my index is int and then put the annotation on the object and when creating the composite index you put the Annotation on your object of type int to contribute to the mapping. I do not know if it will work, because it gave another problem, but in the other question I asked it has an example: http://answall.com/questions/175875/problemas-de-mapping-no-microsoft-entityframeworkcore I do not know if I understood the explanation, had to create a parentid int and put Annotation on it [Foreignkey("Parent")],

  • Rectify the answer to work with example that I accept. And thanks for the help!

  • In the documentation cited in the answer it says that it is not possible to create indexes composed by data annotations, maybe the error is related to this.

  • Not like what I saw, it should have been: {c.Name, c.Parentid} and create Parentid and Parentid have been associated with Parent

  • Well, I edited the question just to clarify. As I was doing I think it is not possible to inform a class to be indexed. If you succeed in the way you posted.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.