为什么 EF 会查找不存在的列?

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

一切都可以编译,但在运行时我得到了

Microsoft.Data.SqlClient.SqlException     
  HResult=0x80131904     
  Message=Invalid column name 'DeviceModelID'.     
  Source=Core Microsoft SqlClient Data Provider     
  StackTrace:     
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)    
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)    

数据库或我的代码中没有DeviceModelID。
我不知道它为什么要找它。
DeviceModel 有一个名为 ID 的 ID。

这是有问题的代码:

public void LoadComponents(IDeviceSummaryContext context) {
    List<ComponentModel> components = 
        context.Components                      |
        .Join(context.DevicesComponents,        |
           c => c.ID,                           |
           dc => dc.ComponentID,                | -- Error here
           (c, dc) => new { c, dc })            |
        .Where(x => x.dc.DeviceID == DeviceID)  |
        .Select(x => x.c).ToList();             |
    foreach(var item in components) {
        Components.Add(item);
    }
}

从这里调用,这样你就可以看到DeviceID来自哪里:

private static DeviceSummaryModel InternalDeviceSummary(IDeviceSummaryContext deviceSummaryContext, QRModel qr, DeviceModel device) {
    DeviceSummaryModel model = new DeviceSummaryModel {
        QRKey = qr.QRKey,
        DeviceID = device.ID,
        TagNumber = device.TagNumber
    };
    model.LoadComponents(deviceSummaryContext);
    return model;
}

连接表的模型:

[PrimaryKey(nameof(ComponentID), nameof(DeviceID))]
public class DevicesComponentsModel {
    //[Key]
    //[Column(Order = 1)]
    [ForeignKey("Components")]
    public int ComponentID { get; set; }

    //[Key]
    //[Column(Order = 2)]
    [ForeignKey("Devices")]
    public int DeviceID { get; set; }
}

上下文:

public interface IDeviceSummaryContext {
    public DbSet<ComponentModel> Components { get; set; }
    public DbSet<DeviceModel> Devices { get; set; }
    public DbSet<DevicesComponentsModel> DevicesComponents { get; set; }
    public DbSet<MasterModel> MasterDocs { get; set; }
    public DbSet<QRModel> QRKeys { get; set; }

}

设备型号。 ComponentModel 类似:

[PrimaryKey(nameof(ID))]  // I tried adding this, but it didn't help.
public class DeviceModel {
    public int ID { get; set; }
    public string TagNumber { get; set; } = "";
    public string QRKey { get; set; } = "";
    public int? DocID { get; set; }
    public int LocationID { get; set; }       
    public ICollection<ComponentModel> Components { get; set; } = [];        
}
c# asp.net-core entity-framework-core
1个回答
0
投票

ForeignKeyAttribute(String)
构造函数接受以下参数:

name
String

关联导航属性的名称,或者一个或多个关联外键的名称。

您的模型没有定义导航属性,请尝试提供它们:

[PrimaryKey(nameof(ComponentID), nameof(DeviceID))]
public class DevicesComponentsModel {
    [ForeignKey(nameof(Component))]
    public int ComponentID { get; set; }

    [ForeignKey(nameof(Device))]
    public int DeviceID { get; set; }

    public ComponentModel Component {get;set;}
    public DeviceModel Device {get;set;}
}

我还建议公开导航属性,这样您就不需要手动编写连接。

另请参阅 EF Core:多对多关系

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