我正在為現有數據庫創建EF7映射,並且我收到“無效列名”錯誤。拋出錯誤的代碼是:
public class DepositContext : DbContext
{
public DbSet<tblBatch> Batches { get; set; }
public DbSet<tblTransaction> Transactions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<tblBatch>().HasKey(b => b.BatchID);
modelBuilder.Entity<tblTransaction>().HasKey(t => t.TransactionID);
modelBuilder.Entity<tblBatch>().HasMany(b => b.tblTransactions).WithOne().HasForeignKey(t => t.fBatchID);
}
}
public class tblBatch
{
public int BatchID { get; set; }
public int? fDepositID { get; set; }
public Guid BRN { get; set; }
public string BCN { get; set; }
public decimal? BCT { get; set; }
public string BatchFileName { get; set; }
public List<tblTransaction> tblTransactions { get; set; }
}
public class tblTransaction
{
public int TransactionID { get; set; }
public string TRN { get; set; }
public string TransactionStatus { get; set; }
public int fBatchID { get; set; }
public tblBatch tblBatch { get; set; }
}
這會拋出一個Invalid column name 'tblBatchBatchID1'.
當我使用SQL事件探查器查看發送到數據庫的內容時,它是:
exec sp_executesql N'SELECT [t].[TransactionID], [t].[fBatchID], [t].[TRN], [t].[TransactionStatus], [t].[tblBatchBatchID1]
FROM [tblTransaction] AS [t]
INNER JOIN (
SELECT DISTINCT TOP(1) [b].[BatchID]
FROM [tblBatch] AS [b]
WHERE [b].[BatchID] = @__BatchId_0
) AS [b] ON [t].[fBatchID] = [b].[BatchID]
ORDER BY [b].[BatchID]',N'@__BatchId_0 int',@__BatchId_0=37
有人知道如何解決這個問題嗎?
EF RC1中存在一些錯誤,這些錯誤在查詢生成中產生了錯誤的列名。查看可能與您的問題相關的錯誤列表 。
您可以通過顯式設置列名來嘗試解決此問題。
modelBuilder.Entity<tblBatch>().Property(e => e.BatchID).HasColumnName("BatchID");
如果您感覺很勇敢,可以嘗試升級到RC Core的RC2夜魘,看看問題是否已解決。