簡體   English   中英

實體框架7無效的列名稱

[英]Entity Framework 7 Invalid Column Name

我正在為現有數據庫創建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");

如果您覺得自己很勇敢,可以嘗試升級到EF Core的RC2每晚,並查看問題是否已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM