简体   繁体   English

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

[英]Entity Framework suggest invalid field name

I have two tables in my DataBase, BUNTS , which contains information about pieces of steel 我的DataBase中有两个表, BUNTS ,其中包含有关钢件的信息

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

and POLL_WEIGHT_BUNTS , which contains information about operations that had been performed on each bunt POLL_WEIGHT_BUNTS ,其中包含有关每个POLL_WEIGHT_BUNTS上执行的操作的信息

CREATE TABLE POLL_WEIGHT_BUNTS (
    PWBCODE            INTEGER NOT NULL,
    PWBBUNTCODE        INTEGER,
    PWBDEPARTMENTFROM  INTEGER,
    PWBDEPARTMENTTO    INTEGER
    ....
);

The relationship is one-to-many. 这种关系是一对多的。 I mapped those tables to models. 我将这些表映射到模型。 Everything worked just fine. 一切都很好。 Recently I've decided to add a field to table BUNTS which would reference to the last operation that had been performed on bunt: 最近我决定在表BUNTS中添加一个字段,该字段将引用在bunt上执行的最后一个操作:

BUNTLASTOPER     INTEGER

Now my models look like this: 现在我的模型看起来像这样:

[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; }
    .....
}

After I've made this, when I try to query Operations like this 在我做完之后,当我尝试查询这样的操作时

return _context.Operations;

it generates an SQL-statement with new incorrect field Bunt_Code 它生成一个带有新的错误字段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"

I assume that now EF looks for a field that is a foreign key for BUNTS table, and cant find it. 我假设现在EF查找的字段是BUNTS表的外键,并且无法找到它。 So it generates Bunt_Code field, which is missing in my database. 所以它会生成Bunt_Code字段,这在我的数据库中是丢失的。 But I already have a property Bunt in BuntOperation class, which references to BUNTS table. 但是我已经在BuntOperation类中有一个属性Bunt ,它引用了BUNTS表。 What am I missing? 我错过了什么?


UPDATE seems like this solves my problem 更新似乎这解决了我的问题

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);
}

seems like this solves my problem 好像这解决了我的问题

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM