Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET添加迁移';复合主键错误';如何使用fluentapi_C#_Asp.net_Entity Framework_Entity Framework Core - Fatal编程技术网

C# ASP.NET添加迁移';复合主键错误';如何使用fluentapi

C# ASP.NET添加迁移';复合主键错误';如何使用fluentapi,c#,asp.net,entity-framework,entity-framework-core,C#,Asp.net,Entity Framework,Entity Framework Core,您好,我正在创建Web应用程序,并且已经安装了Microsoft.entityFrameworkCore和Microsoft.entityFrameworkCore.Tools 在PackageManager控制台中执行添加迁移的过程中,我遇到了一个错误 System.InvalidOperationException:实体类型“Attents”具有用数据批注定义的复合主键。要设置复合主键,请使用fluent API 这是我在实体文件夹中的代码 using System; using Syste

您好,我正在创建Web应用程序,并且已经安装了Microsoft.entityFrameworkCore和Microsoft.entityFrameworkCore.Tools

在PackageManager控制台中执行添加迁移的过程中,我遇到了一个错误

System.InvalidOperationException:实体类型“Attents”具有用数据批注定义的复合主键。要设置复合主键,请使用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; }
    }
}

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

EF core..

复合密钥只能使用Fluent API进行配置- 约定永远不会设置复合键,并且不能使用数据 注释来配置一个

以下是Fluent API的版本:

注意:这只是一个例子。请根据您的使用情况进行调整

// (In the DbContext subclass)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Attends>()
        .HasKey(c => new { c.FarmerSS, c. HotelID });
}
//(在DbContext子类中)
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{

modelBuilder.Entity

你就是那个人!刚刚运行了它,并且在子类上也出现了一个错误。我将继续使用此示例。谢谢!可能值得一提的是,这是在DBContext上,而不是Attents对象上,以防其他人看不到。知道为什么不能使用注释来完成此操作吗?为什么我们要被迫使用fluent API?@PriyankPanchal:因为微软改进了一些东西。显然,它在Core之前工作得很好。