实体框架建议无效的字段名



在我的数据库BUNTS中有两个表,其中包含有关钢材的信息

CREATE TABLE BUNTS (
    BUNTCODE         INTEGER NOT NULL,
    BUNTNAME         VARCHAR(20),
    BUNTSTEEL        INTEGER,
    ......
);

POLL_WEIGHT_BUNTS,其中包含在每个hunt

上执行的操作的信息。
CREATE TABLE POLL_WEIGHT_BUNTS (
    PWBCODE            INTEGER NOT NULL,
    PWBBUNTCODE        INTEGER,
    PWBDEPARTMENTFROM  INTEGER,
    PWBDEPARTMENTTO    INTEGER
    ....
);

关系是一对多。我把这些表映射到模型上。一切都很顺利。最近,我决定在表BUNTS中添加一个字段,该字段将引用上次在hunt上执行的操作:

BUNTLASTOPER     INTEGER

现在我的模型是这样的:

[Table("BUNTS")]
public class Bunt
{
    [Key]
    [Column("BUNTCODE")]
    public int? Code { set; get; }
    [Column("BUNTNAME")]
    public string Name { set; get; }
    [Column("BUNTSTEEL")]
    public int? SteelCode { set; get; }
    [Column("BUNTLASTOPER")]
    public int? LastOperationID { set; get; }
    [ForeignKey("LastOperationID")]
    public BuntOperation LastOperation { set; get; }
    public virtual ICollection<BuntOperation> Operations { set; get; }
}
[Table("POLL_WEIGHT_BUNTS")]
public class BuntOperation
{
    [Key]
    [Column("PWBCODE")]
    public int? Code { set; get; }
    [Column("PWBBUNTCODE")]
    public int? BuntCode { set; get; }
    [ForeignKey("BuntCode")]
    public Bunt Bunt { set; get; }
    [Column("PWBDEPARTMENTFROM")]
    public int? DepartmentFromCode { set; get; }
    .....
}

在我做了这个之后,当我尝试像这样查询操作

return _context.Operations;

它生成一个带有新的错误字段Bunt_Code的sql语句

SELECT 
"B"."PWBCODE" AS "PWBCODE", 
"B"."PWBBUNTCODE" AS "PWBBUNTCODE", 
"B"."PWBDEPARTMENTFROM" AS "PWBDEPARTMENTFROM", 
....
"B"."Bunt_Code" AS "Bunt_Code"
FROM   "POLL_WEIGHT_BUNTS" AS "B"

我假设现在EF寻找一个字段是BUNTS表的外键,并且找不到它。因此,它生成Bunt_Code字段,这是在我的数据库中缺失的。但是我已经在BuntOperation类中有一个属性Bunt,它引用了BUNTS表。我错过了什么?


更新似乎这解决了我的问题

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Bunt>().HasOptional(b => b.LastOperation).WithMany();
    modelBuilder.Entity<Bunt>().HasMany(b => b.Operations).WithRequired(op => op.Bunt);
}

似乎这解决了我的问题

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Bunt>().HasOptional(b => b.LastOperation).WithMany();
     modelBuilder.Entity<Bunt>().HasMany(b => b.Operations).WithRequired(op => op.Bunt);
}

相关内容

  • 没有找到相关文章

最新更新