实体框架核心创建其他表列

问题描述 投票:0回答:4

我正在尝试迁移一个简单的表,但是ef-core正在创建我未指定的其他列

这是我用来创建sql表的模型

public class Transaction
{
        [Key]
        public int Id { get; set; }

        [Required]
        public decimal Amount { get; set; }

        [Required]
        public DateTime DateTimeCreated { get; set; } = DateTime.UtcNow;

        [MaxLength(1024)]
        public string Description { get; set; }

        [Required]
        public int CategoryId { get; set; }

        public virtual Category Category { get; set; }

        [Required]
        public int UserId { get; set; }

        public virtual User User { get; set; }
    }

EF-Core正在创建下表。

如何阻止EF-Core生成CategoryName和CategoryCashFlowId列

migrationBuilder.CreateTable(
                name: "Transactions",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Amount = table.Column<decimal>(nullable: false),
                    DateTimeCreated = table.Column<DateTime>(nullable: false),
                    Description = table.Column<string>(maxLength: 1024, nullable: true),
                    CategoryId = table.Column<int>(nullable: false),
                    CategoryName = table.Column<string>(nullable: false),
                    CategoryCashFlowId = table.Column<int>(nullable: false),
                    UserId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Transactions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Transactions_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_Transactions_Categories_CategoryName_CategoryCashFlowId",
                        columns: x => new { x.CategoryName, x.CategoryCashFlowId },
                        principalTable: "Categories",
                        principalColumns: new[] { "Name", "CashFlowId" },
                        onDelete: ReferentialAction.Cascade);
                });

这是我的流利的api代码

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<User>().HasIndex("Username").IsUnique();
            modelBuilder.Entity<Category>().HasKey(c => new { c.Name, c.CashFlowId });

            modelBuilder.Entity<Common.Models.CashFlow>().HasData(
                new CashFlow { Id = 1, Name = "Income" },
                new CashFlow { Id = 2, Name = "Expense" }
            );

            modelBuilder.Entity<Common.Models.Category>().HasData(
                new Category { Id = 1, Name = "Food and Drinks", CashFlowId = 2 },
                new Category { Id = 2, Name = "Travel", CashFlowId = 2 },
                new Category { Id = 3, Name = "Salary", CashFlowId = 1 }
            );
        }

编辑1:类别模型

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

        /// <summary>
        /// <remarks> Is Indexed with CashflowId </remarks>
        /// </summary>
        [MaxLength(32)]
        [Required]
        public string Name { get; set; }

        [Required]
        public int CashFlowId { get; set; }

        public CashFlow CashFlow { get; set; }

}

编辑2:添加的CashFlow模型

public class CashFlow
{
        [Key]
        public int Id { get; set; }

        [MaxLength(32)]
        public string Name { get; set; }
}

编辑3:发布了我的DbContext

public class AppDbContext : DbContext
    {

        public DbSet<User> Users { get; set; }

        public DbSet<Common.Models.Transaction> Transactions { get; set; }

        public DbSet<Category> Categories { get; set; }

        public DbSet<CashFlow> CashFlows { get; set; }

        public AppDbContext(DbContextOptions<AppDbContext> dbContextOptions) : base(dbContextOptions)
        {
            ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<User>().HasIndex("Username").IsUnique();
            modelBuilder.Entity<Category>().HasKey(c => new { c.Name, c.CashFlowId });

            modelBuilder.Entity<Common.Models.CashFlow>().HasData(
                new CashFlow { Id = 1, Name = "Income" },
                new CashFlow { Id = 2, Name = "Expense" }
            );

            modelBuilder.Entity<Common.Models.Category>().HasData(
                new Category { Id = 1, Name = "Food and Drinks", CashFlowId = 2 },
                new Category { Id = 2, Name = "Travel", CashFlowId = 2 },
                new Category { Id = 3, Name = "Salary", CashFlowId = 1 }
            );
        }
    }

我不明白为什么要创建CategoryNameCategoryCashFlowId

请帮助

c# asp.net-core entity-framework-core ef-fluent-api
4个回答
1
投票

为什么要创建CategoryName和CategoryCashFlowId列


0
投票

当您使用此配置时


0
投票

我相信这是因为您的Transaction模型与Category一起具有CategoryId模型的引用(导航属性)。但是它实际上不能由Category引用CategoryId模型,因为Category模型实际上具有NameCashFlowId的复合键。因此,Entity Framework将这些添加到幕后以供参考。


0
投票

类别上的现金流应该是虚拟的。

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