我有一个使用EF 6的代码第一个数据库。
我正面对similar issue。我尝试了所有的答案,但没有一个成功。
我的实体是:
public class UtilizationSummary
{
public DateTime SummaryDate { get; set; }
public Int32 ProcessKey { get; set; }
public virtual ProcessInfo ProcessInfo { get; set; }
public Guid MachineId { get; set; }
public virtual Machine Machine { get; set; }
}
public class ProcessInfo
{
public virtual Machine Machine { get; set; }
public virtual Session Session { get; set; }
}
public class Machine
{
public Guid Id { get; set; }
public String Sid { get; set; }
}
映射如下:
public class UtilizationSummaryMap: EntityTypeConfiguration<UtilizationSummary>
{
this.HasKey(pus=> new { pus.MachineId, pus.SummaryDate, pus.ProcessKey });
this.Property(pus=> pus.MachineId).IsRequired();
this.HasRequired(pus=> pus.Machine).WithMany().HasForeignKey(ma => ma.MachineId);
}
使用:
访问[HttpGet]
[ODataRoute("UtilizationSummary")]
[EnableQuery]
public IQueryable<UtilizationSummary> FilterUtilizationSummary()
{
var expression = ...some expression..;
var result = db.UtilizationSummary.Where(expression);
return result;
}
“返回结果”抛出以下异常:
"message":"Invalid column name 'Machine_Id'.","type":"System.Data.SqlClient.SqlException"
打印result
给出:
SELECT
[Extent1].[ProcessKey] AS [ProcessKey],
[Extent1].[MachineId] AS [MachineId],
[Extent1].[SummaryDate] AS [SummaryDate],
[Extent1].[Machine_Id] AS [Machine_Id]
FROM [ProcessUtilizationMinuteSummary] AS [Extent1]
虽然我已经为foriegn密钥提供了映射,但我没有理解为什么它试图访问Machine_Id
。
提前致谢:)