1

Обновляю Asp.net MVC на Core

public class Client
    {
        [Key]
        [ForeignKey("Virtual_KEY")]
        [Column(Order = 0)]
        public int Id { get; set; }
        [Key]
        [Column(Order = 1)]
        public string Id2 { get; set; }

Entity type 'Client' has composite primary key defined with data annotations. To set composite primary key, use fluent API.

4
  • Так вот же и ответ - составной ключ через Fluent API задается
    – Mikhail M
    26 апр 2017 в 17:01
  • так он формируется автоматически и после обновления моделей изменения потеряются? Больше через Column(Order работать не будет?
    – code211218
    26 апр 2017 в 17:02
  • к сожалению у Core есть свои ограничения
    – Mikhail M
    26 апр 2017 в 17:04
  • а где прочитать почему изменили?
    – code211218
    26 апр 2017 в 17:05

1 ответ 1

3

В тот класс, который является у Вас наследником DbContext, нужно добавить такой метод:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Client>()
        .HasKey(c => new { c.Id, c.Id2 });
}

Например:

    public class ApplicationContext : DbContext {
        public DbSet<Client> clientItem { get; set; }

        public ApplicationContext(DbContextOptions<ApplicationContext> options)
            : base(options) {
            Database.EnsureCreated();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder.Entity<TuGroupItem>()
                .HasKey(c => new { c.GroupId, c.TuId });
        }
    }
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.