EF核心3.0问题,外键部署到SQL Server

问题描述 投票:1回答:1

我在.NET Core和EF Core 3.0中拥有其余的API,我遇到了一个奇怪的问题。以下是我的实体。

[Table(name: "BizObjects")]
public class BizObject
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Required]
    [StringLength(06)]
    public int Id { get; set; }
    public string Category { get; set; } //FK

    [MaxLength(2)]
    public int ItemIncrement { get; set; }

    [MaxLength(2)]
    public int SubItemIncrement { get; set; }
    public virtual BizObjectT BizObjectT { get; set; }
}

[Table(name: "BizObjectsT")]
public class BizObjectT
{
    [StringLength(02)]
    public string Lang { get; set; }
    public string Text { get; set; }

    #region Primary Key derived from Foreign key for Config tabels                
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    [ForeignKey(name: "BizObjectId")]
    [Required]
    public int BizObjectId { get; set; }
    public virtual BizObject BizObject { get; set; }       
    #endregion
}

这是我的DbContext。

public class ApplicationContext : DbContext, IDisposable
{
    public ApplicationContext(DbContextOptions options)
        : base(options: options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BizObject>()
            .HasOne<BizObjectT>(s => s.BizObjectT)
            .WithOne(g => g.BizObject)
            .HasForeignKey<BizObjectT>(s => s.BizObjectId);
    }

    public DbSet<BizObject> BizObjects { get; set; }
    public DbSet<BizObjectT> BizObjectsT { get; set; }
}

现在,一切正常。我能够创建迁移并将其部署到数据库。我的外键在API中可以正常工作。

但是我遇到的问题是SQL Server。在这里,我无法在实现的关系窗口中找到外键主表。即使尝试手动添加FK,我也看不到该表的状态。

请参见下面的快照。enter image description here

我不确定问题出在哪里,任何建议都会有很大帮助。

谢谢。

更新1:

这里是实体生成的迁移。

public partial class initial : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "BizObjects",
            columns: table => new
            {
                Id = table.Column<int>(maxLength: 6, nullable: false),
                Category = table.Column<string>(nullable: true),
                ItemIncrement = table.Column<int>(maxLength: 2, nullable: false),
                SubItemIncrement = table.Column<int>(maxLength: 2, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_BizObjects", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "BizObjectsT",
            columns: table => new
            {
                BizObjectId = table.Column<int>(nullable: false),
                Lang = table.Column<string>(maxLength: 2, nullable: true),
                Text = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_BizObjectsT", x => x.BizObjectId);
                table.ForeignKey(
                    name: "FK_BizObjectsT_BizObjects_BizObjectId",
                    column: x => x.BizObjectId,
                    principalTable: "BizObjects",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "BizObjectsT");

        migrationBuilder.DropTable(
            name: "BizObjects");
    }
}

奇怪的是,如果我更改数据库名称,它将起作用。但仅适用于前几张桌子。

假设我有10个表要迁移,而这个[[BizObject是第一个表,那么更改数据库名称后,SQL将引用BizObject,但没有其他表的引用,这是随机的。

供参考:

  • VS 2019版本16.3.0
  • SSMS版本14.0
  • SQL Server 2014版本12.0

Rest API配置:

  • 。NET CORE 3.0
  • EF Core 3.0

今天,我将使用SQL Server和SSMS的新版本检查所有这一切,稍后将在此处发布更新...

c# sql-server asp.net-core ef-fluent-api ef-core-3.0
1个回答
0
投票
您的FK注释不是关闭的吗?代替

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)] [ForeignKey(name: "BizObjectId")] [Required] public int BizObjectId { get; set; } public virtual BizObject BizObject { get; set; }

不应该是这样:

[Required] public int BizObjectId { get; set; } [ForeignKey(name: "BizObjectId")] public virtual BizObject BizObject { get; set; }

参见:EF Core Doc

我还删除了[Key]属性,因为我非常确定它不能同时是键和外键-如果愿意,可以添加唯一索引。或者,如果确实将其用作PK(1:1),则应删除[DatabaseGenerated]属性,因为在子记录中它不能由DB生成。

© www.soinside.com 2019 - 2024. All rights reserved.