programing

ASP.NET 마이그레이션 '복합 기본 키 오류' 추가 유창한 API 사용 방법

copyandpastes 2023. 6. 15. 23:11
반응형

ASP.NET 마이그레이션 '복합 기본 키 오류' 추가 유창한 API 사용 방법

안녕하세요. 웹 응용 프로그램을 만드는 중이며 Microsoft.entityFrameworkCoreMicrosoft.entityFrameworkCore를 이미 설치했습니다.도구.

패키지 관리자 콘솔에서 마이그레이션 추가를 실행하는 동안 오류가 발생함

"시스템.잘못된 작업예외: 엔티티 유형 '참석'에 데이터 주석으로 정의된 복합 기본 키가 있습니다. 복합 기본 키를 설정하려면 유창한 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; }
    }
}

컴포지트 키를 올바르게 설정하려면 어떻게 조정해야 합니까?

EF 코어에서 ..

복합 키는 Fluent API를 사용해야만 구성할 수 있습니다. 규약에서는 복합 키를 설정하지 않으며 데이터 주석을 사용하여 구성할 수 없습니다.

Fluent API 버전은 다음과 같습니다.

참고: 이것은 단지 예시일 뿐입니다.사용 사례에 따라 조정해 주시기 바랍니다.

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

자세한 내용은 여기에서 확인할 수 있습니다. 복합

언급URL : https://stackoverflow.com/questions/40898365/asp-net-add-migration-composite-primary-key-error-how-to-use-fluent-api

반응형