0

I have an existing model, SomeModel, which contains a property Prop. I'd like to enforce that the values of Prop be unique.

To this end, I'm adding the following to my model:

public static void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<SomeModel>()
      .HasAlternateKey(a => a.Prop);
}

but ef migrations isn't picking up the change.

How can I correct this?

Isaac Kleinman
  • 3,994
  • 3
  • 31
  • 35

1 Answers1

1

This works for me with EF Core on an already existing model. add-migration picks up the change.

public static void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<SomeModel>()
                .HasIndex(u => u.Prop)
                .IsUnique();
}
Collin Barrett
  • 2,441
  • 5
  • 32
  • 53
  • I've found that this override only works if it's there when you first create the model - not if you add it at some later point. – Isaac Kleinman Sep 20 '18 at 16:49
  • Hmm, you are using EF Core, correct (inferred from the tag)? I just tested it on an existing model in one of my projects, and this picked it up just fine. Maybe it depends on your EF provider? My example: https://github.com/collinbarrett/FilterLists/commit/d4c2198791e657cc20bdb3d0a51490a0d02ad9c5 – Collin Barrett Sep 20 '18 at 18:17