你好,我正在创建一个Web应用程序,并且已经安装了Microsoft.entityFrameworkCoreMicrosoft.entityFrameworkCore.Tools.

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

"System.InvalidOperationException: Entity type 'Attends' has composite primary key defined with data annotations. To set composite primary key, use fluent API"

以下是我在Entity文件夹中的代码.

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

你可以在这里阅读更多信息:composite key

Asp.net相关问答推荐

.net 中的 [] 括号是什么?

为什么 Web 架构应该是松耦合的?

无法加载文件或程序集.无效指针(HRESULT 异常:0x80004003 (E_POINTER))

HttpRuntime.Cache[] 与 Application[]

如何调试 Azure 500 内部服务器错误

ASP.NET Response.Redirect 使用 302 而不是 301

ASP.NET Membership:如何将用户设置为已登录

如何使用 javascript 获取 MVC 应用程序的基本 URL

如何从网页 (asp.net) 启动 EXE

没有身份的 ASP.NET Core 2.0 承载身份验证

使用 JavaScript 禁用 ASP.NET 验证器

是否有用于 Asp.net 标记的#IF DEBUG?

如何在 ASP.Net Core Razor 页面上重定向

每个 'HttpRequest' 在 ASP.NET 中都有自己的线程吗?

如何在asp.net中获取应用程序路径?

oAuth ASP.NET 成员资格提供程序

如何在 ASP.NET MVC 中启用跨源请求

禁用 web.config 继承?

ASP.NET:在 Response.Redirect(...) 之后代码会发生什么?

ASP.NET 网格视图与列表视图