我正在尝试迁移一个简单的表,但是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 }
);
}
}
我不明白为什么要创建CategoryName和CategoryCashFlowId列
请帮助
为什么要创建CategoryName和CategoryCashFlowId列
当您使用此配置时
我相信这是因为您的Transaction
模型与Category
一起具有CategoryId
模型的引用(导航属性)。但是它实际上不能由Category
引用CategoryId
模型,因为Category
模型实际上具有Name
和CashFlowId
的复合键。因此,Entity Framework将这些添加到幕后以供参考。
类别上的现金流应该是虚拟的。