【问题标题】:ASP.NET add migration 'composite primary key error' how to use fluent APIASP.NET添加迁移'复合主键错误'如何使用fluent API
【发布时间】:2017-04-15 08:47:49
【问题描述】:

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

在包管理器控制台中执行添加迁移的过程中出现错误

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

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

【问题讨论】:

    标签: c# asp.net entity-framework entity-framework-core


    【解决方案1】:

    EF核心..

    只能使用 Fluent API 配置复合键 - 约定永远不会设置复合键,您不能使用 Data 配置一个注解。

    这里是 Fluent API 版本:

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

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

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

    【讨论】:

    • 你就是男人!刚刚运行它并且在 Child 类上也出现了错误。我将继续使用这个例子。谢谢!
    • 可能值得一提的是,这是在 DBContext 上,而不是在 Attends 对象上,以防其他人看不出来。
    • 知道为什么不能使用注释来完成吗?为什么我们要强制使用 Fluent API?
    • @PriyankPanchal:因为微软改进了的东西。显然,它在 Core 之前运行良好。
    猜你喜欢
    • 2014-08-22
    • 2022-12-12
    • 2017-09-28
    • 1970-01-01
    • 2019-01-30
    • 2019-07-24
    • 2017-02-09
    • 1970-01-01
    • 2019-02-09
    相关资源
    最近更新 更多