ASP.NET添加迁移“复合主键错误”如何使用流畅的API


76

您好,我正在创建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; }
    }
}

如何调整它以正确设置合成键?

Answers:


170

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

您可以在此处阅读有关此内容的更多信息:复合键


2
你是男人!只需运行它,在Child类上也会出错。我将继续使用这个示例。谢谢!
RyeGuy

18
可能值得一提的是,这在DBContext上而不是Attends对象上,以防对其他人不明显。
kiml42

5
知道为什么不能使用注释完成此操作吗?为什么我们被迫使用流利的API?
Priyank Panchal

@PriyankPanchal:因为微软改进了。显然,它在Core之前运行良好。
乔纳森·伍德
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.