Entity Framework 7 Invalid Column Name

亡梦爱人 提交于 2019-12-24 15:59:19

问题


I'm creating EF7 mappings for an existing database and I'm getting an "Invalid column name" error. The code that is throwing the error is:

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

This is throwing a Invalid column name 'tblBatchBatchID1'. And when I use SQL Profiler to see what is being sent to the database, it is:

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

Does anybody know how to fix this?


回答1:


There were some bugs in EF RC1 that produced the wrong column name in query generation. See this list of bugs that might be related to your issue.

You can attempt to work around this by explicitly setting the column name.

modelBuilder.Entity<tblBatch>().Property(e => e.BatchID).HasColumnName("BatchID");

If you're feeling brave, you can try upgrading to RC2 nightlies of EF Core and see if the issue is fixed there.



来源:https://stackoverflow.com/questions/36413461/entity-framework-7-invalid-column-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!