实体框架代码第一个无效的列名称Company_CompanyID

时间:2014-02-13 14:03:58

标签: entity-framework ef-code-first

我第一次涉足Code First。我的大多数表都运行正常,但在查询名为Bids的表时,我收到了“无效的列名Company_CompanyID”。

当我为一个小样本应用程序反向设计数据库时,我没有收到此错误。我已经将Bid类和从示例应用程序到我的实际应用程序的映射进行了比较,但我找不到区别。在我的实际应用程序中为Bids查询生成的SQL如下所示:

SELECT 
    [Extent1].[LockCount] AS [LockCount], 
    [Extent1].[BidID] AS [BidID], 
    [Extent1].[LocaleID] AS [LocaleID], 
    [Extent1].[Description] AS [Description], 
    [Extent1].[FileLocation] AS [FileLocation], 
    [Extent1].[EstimatorContactID] AS [EstimatorContactID], 
    [Extent1].[OwnerContactID] AS [OwnerContactID], 
    [Extent1].[OwnerContactContactID] AS [OwnerContactContactID], 
    [Extent1].[GCContactID] AS [GCContactID], 
    [Extent1].[GCContactContactID] AS [GCContactContactID], 
    [Extent1].[ArchitectContactID] AS [ArchitectContactID], 
    [Extent1].[ArchitectContactContactID] AS [ArchitectContactContactID], 
    [Extent1].[BuilderContactID] AS [BuilderContactID], 
    [Extent1].[BuilderContactContactID] AS [BuilderContactContactID], 
    [Extent1].[DrawingNumber] AS [DrawingNumber], 
    [Extent1].[BidDate] AS [BidDate], 
    [Extent1].[PlanDate] AS [PlanDate], 
    [Extent1].[RevisionDate] AS [RevisionDate], 
    [Extent1].[StartDate] AS [StartDate], 
    [Extent1].[FinishDate] AS [FinishDate], 
    [Extent1].[Address1] AS [Address1], 
    [Extent1].[Address2] AS [Address2], 
    [Extent1].[City] AS [City], 
    [Extent1].[State] AS [State], 
    [Extent1].[PostalCode] AS [PostalCode], 
    [Extent1].[J1] AS [J1], 
    [Extent1].[J2] AS [J2], 
    [Extent1].[J3] AS [J3], 
    [Extent1].[J4] AS [J4], 
    [Extent1].[J5] AS [J5], 
    [Extent1].[J6] AS [J6], 
    [Extent1].[J7] AS [J7], 
    [Extent1].[J8] AS [J8], 
    [Extent1].[PriceIndex] AS [PriceIndex], 
    [Extent1].[Notes] AS [Notes], 
    [Extent1].[Proposal] AS [Proposal], 
    [Extent1].[JobOptions] AS [JobOptions], 
    [Extent1].[LastEstimatedDate] AS [LastEstimatedDate], 
    [Extent1].[IsTemplate] AS [IsTemplate], 
    [Extent1].[Awarded] AS [Awarded], 
    [Extent1].[JobStatus] AS [JobStatus], 
    [Extent1].[LockedByUserID] AS [LockedByUserID], 
    [Extent1].[LockedDate] AS [LockedDate], 
    [Extent1].[RowVersion] AS [RowVersion], 
    [Extent1].[Company_CompanyID] AS [Company_CompanyID]
    FROM [dbo].[Bids] AS [Extent1]
    WHERE (([Extent1].[LockedByUserID] = @p__linq__0) AND ( NOT ([Extent1].[LockedByUserID] IS NULL OR @p__linq__0 IS NULL))) OR (([Extent1].[LockedByUserID] IS NULL) AND (@p__linq__0 IS NULL))

我的Bids表中的所有列都显示在查询中,另外还有Company_CompanyID。我不确定如何试图找出映射到Company_CompanyID的内容。有人可以提供一些指示,说明如何弄清楚导致Company_CompanyID进入的原因是什么?非常感谢!

1 个答案:

答案 0 :(得分:1)

这是一个Foreign Key列,用于将Bids表中的行与Companies表中的行相关联。默认情况下,Entity Framework使用以下模式生成外键名称:

{navigationPropertyName}_{primaryKeyPropertyNameOfReferencedEntity}

即。如果你有公司实体:

public class Company
{
    public int CompanyID { get; set; }
    // ...
}

具有公司导航属性的出价:

public class Bid
{
    public int BidID { get; set; }
    public virtual Company Company { get; set; }
    // ...
}

然后为employee表生成的外键字段名称将显示为

Company_CompanyID