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.
– novic
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.
– novic
@Virgilionovic thanks, sorry for the error, I posted at lunch and copied the wrong excerpt I wanted to show. I already edited and corrected.
– Sileno Brito
No need to apologize @Slienobrito normal happens, it was more a warning!
– novic
Is a unique index
pk
? A simple index? Or a composite index?– rubStackOverflow
@rubStackOverflow It is a composite index, I made an error when posting the question and got confused.
– Sileno Brito