web-dev-qa-db-ja.com

ASP.NETは、Fluid APIを使用する方法「複合主キーエラー」の移行を追加します

こんにちは、Webアプリケーションの作成中です。Microsoft.entityFrameworkCoreMicrosoftの両方を既にインストールしています。 entityFrameworkCore.Tools

パッケージマネージャーコンソールで追加移行を実行するプロセス中にエラーが発生します

"System.InvalidOperationException:エンティティタイプ 'Attends'には、データアノテーションで定義された複合プライマリキーがあります。複合プライマリキーを設定するには、fluent APIを使用します"

エンティティフォルダー内のコードを次に示します。

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; }
    }
}

複合キーを適切に設定するためにどのように調整しますか?

52
RyeGuy

オンEFコア..

複合キーはFluent APIを使用してのみ構成できます-規則では複合キーはセットアップされず、データ注釈を使用して構成することはできません。

これがFluent APIバージョンです:

注:これは一例です。ユースケースに応じて調整してください。

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

詳しくはこちらをご覧ください: composite key

120
Sampath