我有一个用户表和一个产品表 表UsersProducts中它们之间的多对多关系。
每个用户都可以请求获得更多产品,我决定在向用户提供产品时保存日期
public class UserProduct
{
public int UserId { get; set; }
public User User { get; set; }
public Product Product { get; set; }
public int ProductId { get; set; }
public DateTime DateAssigned { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserProduct>()
.HasKey(bc => new { UserId = bc.UserId, ProductId = bc.ProductId });
modelBuilder.Entity<UserProduct>()
.HasOne(bc => bc.User)
.WithMany(b => b.Products)
.HasForeignKey(bc => bc.UserId);
modelBuilder.Entity<UserProduct>()
.HasOne(bc => bc.Product)
.WithMany(c => c.Owners)
.HasForeignKey(bc => bc.ProductId);
base.OnModelCreating(modelBuilder);
}
当我尝试访问(UsersProducts.Where(...))时,我会在DateAssigned列中获得“字段列表中的未知列”。
这是迁移中生成的代码EF
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "DateAssigned",
table: "UsersProducts",
type: "datetime(6)",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DateAssigned",
table: "UsersProducts");
}
表中的列似乎存在,那为什么会发生这种情况呢?
答案 0 :(得分:4)
尝试在映射
中添加它 .Property(t => t.DateAssigned).HasColumnName("DateAssigned")