shuhelohelo’s blog

Xamarin.Forms多めです.

Entity Framework CoreでカラムにUnique制約をつける

stackoverflow.com

属性でつけることはできないけれど,FluentAPIでつけることができる.

DbContextクラスのOnModelCreatingメソッド内で以下のようにする.

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<Member>()
                .HasIndex(m => m.LastName)
                .IsUnique();
        }