我的 table 中没有 属性 名称,但出现列名称无效错误

I don't have property name in my table but I am getting invalid column name error

我有一个 IssueActivity 模型,我有一个该模型的 IssueActivityDetail 模型,我将其设置为自父模型,以便它可以成为 IssueActivityDetail 的详细信息。但是即使我没有 IssueActivityId1 列,我也会收到此错误。

public partial class IssueActivitiy
    {    
        public int Id { get; set; }
        public int IssueId { get; set; }
        public int Type { get; set; }
        public short SubActivityNo { get; set; }
        public string SubActivityTitle { get; set; }

        public virtual Issue Issue { get; set; }
        public virtual List<IssueActivitiyDetail> IssueActivitiyDetails { get; set; }
    }
 public partial class IssueActivitiyDetail
    {
        public int Id { get; set; }
        public int IssueActivitiyId { get; set; }
        public string LineNo { get; set; }
        public string Definition { get; set; }
        public byte RoleId { get; set; }
        public string Medium { get; set; }
        public string Explanation { get; set; }
        public int ParentId { get; set; }


        public virtual IssueActivitiy IssueActivitiy { get; set; }

        public virtual List<IssueActivitiyDetail> IssueActivitiyDetails { get; set; }
        
        public virtual IssueActivitiyDetail Parent { get; set; }
    }

我在 ef core 的 FLuentApiConfiguration IssueActivitiy

modelBuilder.HasOne(d => d.Issue)
                .WithMany(p => p.IssueActivitiys)
                .HasForeignKey(d => d.IssueId).OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_Issue_IssueActivitiy_Id");

            modelBuilder.HasMany(z => z.IssueActivitiyDetails)
               .WithOne()
               .HasForeignKey(z=> z.IssueActivitiyId);

我的 FLuentApiConfiguration IssueActivitiyDetailat ef core

 modelBuilder
                .HasMany(z => z.IssueActivitiyDetails)
                .WithOne(z => z.Parent)
                .HasForeignKey(z => z.ParentId);

我的尝试: 我使用这种 DbContext:

public class TestDbContext : DbContext
{
    public DbSet<Issue> Issues { get; set; }
    public DbSet<IssueActivitiy> IssueActivities { get; set; }
    public DbSet<IssueActivitiyDetail> IssueActivitiyDetails { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseSqlite(CreateInMemoryDatabase())
            .LogTo(s => Debug.WriteLine(s));

        base.OnConfiguring(optionsBuilder);
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IssueActivitiyDetail>()
            .HasMany(d => d.IssueActivitiyDetails)
            .WithOne(p => p.Parent);

        base.OnModelCreating(modelBuilder);
    }

    private static DbConnection CreateInMemoryDatabase()
    {
        var connection = new SqliteConnection("Filename=:memory:");
        connection.Open();
        return connection;
    }
}

这是我要使用的数据模型类:

public class Issue
{
    public int Id { get; set; }
}

public class IssueActivitiy
{
    public int Id { get; set; }
    public int Type { get; set; }
    public short SubActivityNo { get; set; }
    public string SubActivityTitle { get; set; }

    public Issue Issue { get; set; }
    public List<IssueActivitiyDetail> IssueActivitiyDetails { get; set; }
}

public class IssueActivitiyDetail
{
    public int Id { get; set; }
    public string LineNo { get; set; }
    public string Definition { get; set; }
    public byte RoleId { get; set; }
    public string Medium { get; set; }
    public string Explanation { get; set; }


    public IssueActivitiy IssueActivitiy { get; set; }
    public List<IssueActivitiyDetail> IssueActivitiyDetails { get; set; }
    public IssueActivitiyDetail Parent { get; set; }
}

这是我的迁移结果:

public partial class Initial : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Issues",
            columns: table => new
            {
                Id = table.Column<int>(type: "INTEGER", nullable: false)
                    .Annotation("Sqlite:Autoincrement", true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Issues", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "IssueActivities",
            columns: table => new
            {
                Id = table.Column<int>(type: "INTEGER", nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Type = table.Column<int>(type: "INTEGER", nullable: false),
                SubActivityNo = table.Column<short>(type: "INTEGER", nullable: false),
                SubActivityTitle = table.Column<string>(type: "TEXT", nullable: true),
                IssueId = table.Column<int>(type: "INTEGER", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_IssueActivities", x => x.Id);
                table.ForeignKey(
                    name: "FK_IssueActivities_Issues_IssueId",
                    column: x => x.IssueId,
                    principalTable: "Issues",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "IssueActivitiyDetails",
            columns: table => new
            {
                Id = table.Column<int>(type: "INTEGER", nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                LineNo = table.Column<string>(type: "TEXT", nullable: true),
                Definition = table.Column<string>(type: "TEXT", nullable: true),
                RoleId = table.Column<byte>(type: "INTEGER", nullable: false),
                Medium = table.Column<string>(type: "TEXT", nullable: true),
                Explanation = table.Column<string>(type: "TEXT", nullable: true),
                IssueActivitiyId = table.Column<int>(type: "INTEGER", nullable: true),
                ParentId = table.Column<int>(type: "INTEGER", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_IssueActivitiyDetails", x => x.Id);
                table.ForeignKey(
                    name: "FK_IssueActivitiyDetails_IssueActivities_IssueActivitiyId",
                    column: x => x.IssueActivitiyId,
                    principalTable: "IssueActivities",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_IssueActivitiyDetails_IssueActivitiyDetails_ParentId",
                    column: x => x.ParentId,
                    principalTable: "IssueActivitiyDetails",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_IssueActivities_IssueId",
            table: "IssueActivities",
            column: "IssueId");

        migrationBuilder.CreateIndex(
            name: "IX_IssueActivitiyDetails_IssueActivitiyId",
            table: "IssueActivitiyDetails",
            column: "IssueActivitiyId");

        migrationBuilder.CreateIndex(
            name: "IX_IssueActivitiyDetails_ParentId",
            table: "IssueActivitiyDetails",
            column: "ParentId");
    }

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

        migrationBuilder.DropTable(
            name: "IssueActivities");

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

因此,也许这里简单性会更胜一筹,因为 efcore5 会为我们做很多事情。 所以我不确定生成的数据库结构是否符合您的期望,但到目前为止 works/compiles。