반응형
ASP.NET 마이그레이션 '복합 기본 키 오류' 추가 유창한 API 사용 방법
안녕하세요. 웹 응용 프로그램을 만드는 중이며 Microsoft.entityFrameworkCore와 Microsoft.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
반응형
'programing' 카테고리의 다른 글
명령줄 출력 업데이트(즉, 진행 (0) | 2023.07.25 |
---|---|
%in%의 반대: 벡터에 지정된 값이 있는 행 제외 (0) | 2023.06.15 |
Oracle에서 보기를 만드는 데 사용되는 SQL을 검색하는 방법은 무엇입니까? (0) | 2023.06.15 |
원격 호스트가 연결을 닫았습니다.오류 코드는 0x800704입니다.CD (0) | 2023.06.15 |
두 테이블을 비교하고 경기를 건너뛰는 방법은 무엇입니까? (0) | 2023.06.15 |