2016-11-30 6 views
17

Witam Jestem w trakcie tworzenia aplikacji WWW i został już zainstalowany zarówno Microsoft.entityFrameworkCore i Microsoft.entityFrameworkCore.Tools.ASP.NET dodać migrację „kompozytową podstawowy błąd klucza” Jak stosować Fluent API

Podczas wykonywania dodatek migracji w konsoli Menedżer pakietu pojawia się błąd

"System.InvalidOperationException:. Typ jednostki Uczęszcza" ma zdefiniowany klucz podstawowy kompozytowych z adnotacji danych Aby ustawić pierwszorzędowego złożonego klucz, użyj płynnego API "

Oto mój kod w folderze encji.

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Threading.Tasks; 

namespace _3241_farmDb.Entities 
{ 

    public class Farm 
    { 
     [Required, MaxLength(30)] 
     [Key] 
     public string FarmName { get; set; } 
     [Required, MaxLength(15)] 
     public string FarmCity { get; set; } 
     [Required, MaxLength(9)] 
     public string FarmerSSN { get; set; } 
    } 
    public class Farmer 
    { 
     [Required, MaxLength(9)] 
     [Key] 
     public int SS { get; set; } 
     [Required, MaxLength(9)] 
     public string Fname { get; set; } 
     [Required, MaxLength(15)] 
     public string Lname { get; set; } 
     [Required, MaxLength(15)] 
     public string CityName { get; set; } 
     [Required, MaxLength(15)] 
     public string Address { get; set; } 
     [Required, MaxLength(30)] 
     public string BoardPositionName { get; set; } 
    } 
    public class Child 
    { 
     [Required, MaxLength(9)] 
     [Key] 
     public int FarmerSS { get; set; } 
     [Required, MaxLength(15)] 
     [Key] 
     public string Fname { get; set; } 
     [Required, MaxLength(15)] 
     [Key] 
     public string Lname { get; set; } 
     [Required] 
     public int Age { get; set; } 
    } 
    public class Attends 
    { 

     [Key, Column(Order = 1)] 
     public int FarmerSS { get; set; } 
     [Key, Column(Order = 2)] 
     public int HotelID { get; set; } 
     [Required, MaxLength(15)] 
     public string BoardPosition { get; set; } 
    } 

    public class Livestock 
    { 
     [Required, MaxLength(15)] 
     public int LivestockID { get; set; } 
     [Required, MaxLength(15)] 
     public string LivestockType { get; set; } 
    } 
    public class Farm_Houses 
    { 
     [Required, MaxLength(15)] 
     [Key] 
     public int LivestockID { get; set; } 
     [Required, MaxLength(15)] 
     public string FarmName { get; set; } 
    } 
    public class Crops 
    { 
     [Required, MaxLength(15)] 
     [Key] 
     public int CropID { get; set; } 
     [Required, MaxLength(15)] 
     public string CropName { get; set; } 
    } 
} 

Jak ustawić, aby poprawnie ustawić klucz złożony?

Odpowiedz

42

Na EF rdzenia ..

klucze kompozytowe mogą być konfigurowane tylko za pomocą Fluent API - konwencje nigdy nie będzie ustawienie klucza kompozytowej i nie można korzystać z danych adnotacje skonfigurować jeden.

Oto Fluent API wersja:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Attends>() 
      .HasKey(c => new { c.FarmerSS, c. HotelID }); 
    } 

Możesz przeczytać więcej na ten temat tutaj: composite key

+0

Ty jesteś tym człowiekiem! Po prostu uruchomiłem go i otrzymałem błąd również na klasie Dziecko. Zamierzam użyć tego przykładu do przodu. Dziękuję Ci! – RyeGuy

+1

Serdecznie witamy :) – Sampath

Powiązane problemy