实体框架7列名无效 [英] Entity Framework 7 Invalid Column Name

查看:249
本文介绍了实体框架7列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为现有的数据库创建EF7映射,并且收到无效的列名称错误。抛出错误的代码是:

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

这是抛出一个无效列名'tblBatchBatchID1 '。当我使用SQL Profiler查看发送到数据库的内容时,它是:

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?

推荐答案

EF RC1中有一些错误在查询生成中产生错误的列名称。请参阅这个错误列表可能与您的问题有关。

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");

如果你感到勇敢,可以尝试升级到EF Core的RC2夜总会,看看问题是固定的。

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

这篇关于实体框架7列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆