实体框架 - 数据库优先 - 无效的列名错误

时间:2015-12-11 01:15:35

标签: entity-framework-6 database-first dbset

我有三个简单的类,我正在将EF6连接到现有数据库。

课程如下

namespace Infrastructure.Models
{

    [Table("Applications")]
    public class Application
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid ApplicationID { get; set; }
        public DateTime DateTime { get; set; }
        public string CompletedZipFileURL { get; set; }
        public virtual BusinessInfo BusinessInfo { get; set; }

        public Application()
        {
            this.ApplicationID = Guid.NewGuid();
            this.DateTime = DateTime.Now;
            this.CompletedZipFileURL = string.Empty;
            this.BusinessInfo = new BusinessInfo();
            this.BusinessInfo.ApplicationID = this.ApplicationID;
        }

    }


    [Table("BusinessInfo")]
    public class BusinessInfo
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid BusinessID { get; set; }
        public Guid ApplicationID { get; set; }
        public  string BusinessName { get; set; }
        public  string BusinessType { get; set; }
        public  string StreetAddress { get; set; }
        public  string City { get; set; }
        public  string State { get; set; }
        public  string Zip { get; set; }
        public  string BusinessTelephone { get; set; }
        public  string FEIN { get; set; }
        public  string ILSalesTaxNo { get; set; }
        public  string IncorporateDate { get; set; }
        public virtual ApplicantInfo ApplicantInfo {get;set;}

        public BusinessInfo()
        {
            this.BusinessID = Guid.NewGuid();
            this.ApplicantInfo = new ApplicantInfo();
            this.ApplicantInfo.BusinessID = this.BusinessID;
        }

    }


    public class ApplicantInfo
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid ApplicantID { get; set; }

        public  Guid BusinessID { get; set; }
        public  string Name { get; set; }
        public  string Title { get; set; }
        public  string HomeAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public  string EmailAddress { get; set; }
        public  string PhoneNo { get; set; }
        public  string Criminal { get; set; }

        public ApplicantInfo()
        {
            this.ApplicantID = Guid.NewGuid();
        }

    }

}

My Context Class如下所示:

    public class SIDEntities : DbContext
    {

        public SIDEntities() : base(Settings.GetSetting("ConnectionString"))
        {
            base.Configuration.ProxyCreationEnabled = false;
            base.Configuration.LazyLoadingEnabled = false;
        }

        public virtual DbSet<Infrastructure.Models.Application> Application { get; set; }
        public virtual DbSet<Infrastructure.Models.BusinessInfo> BusinessInfo { get; set; }
        public virtual DbSet<Infrastructure.Models.ApplicantInfo> ApplicantInfo { get; set; }


    }

在我现有的数据库中,我有以下表名和字段:

应用程序(ApplicationID:uniqueidentifier,DateTime:datetime,CompletedZipFileURL:varchar(500))

BusinessInfo(BusinessID:uniqueidentifier,ApplicationID:uniqueidentifier,...)

ApplicationInfo(ApplicantID:uniqueidentifier,BusinessID:uniqueidentifier,...)

出于某种原因,只要我尝试对根应用程序POCO进行查询,我就会收到一个错误,其结果是&#34; {&#34;无效的列名称&#39; BusinessInfo_BusinessID&#39 ;&#34;}&#34;

我试图调试此问题,检查各种SO帖子,但示例/修复不适用于我的数据库第一个场景。

抛出异常的查询是:

    public static Infrastructure.Models.Application Find(Guid id)
    {
        using (SIDEntities cntx = new SIDEntities())
        {
            Infrastructure.Models.Application x = new Infrastructure.Models.Application();
            //the line below is where the error occurs
            x = cntx.Application.Where(m => m.ApplicationID == id).SingleOrDefault();
            return x;
        }
    }

我可以在调试时看到从LINQ生成的查询如下

SELECT     1 AS [C1],     
        [Extent1].[ApplicationID] AS [ApplicationID],     
        [Extent1].[DateTime] AS [DateTime],     
        [Extent1].[CompletedZipFileURL] AS [CompletedZipFileURL],     
        [Extent1].[BusinessInfo_BusinessID] AS [BusinessInfo_BusinessID]    
FROM [dbo].[Applications] AS [Extent1]

我理解为什么我收到错误,因为没有&#34; BusinessInfo_BusinessID&#34; “应用程序”表中的列。

我非常感谢能够获得这方面的任何帮助/指示。

2 个答案:

答案 0 :(得分:2)

检查出来

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid BusinessID { get; set; }

在您的查询中,将 Where SingleOrDefault 更改为:

x = cntx.Application.SingleOrDefault(m => m.ApplicationID == id);

希望有所帮助

答案 1 :(得分:0)

我发现因为我有一对一的关系(在技术上并不存在于SQL服务器上,所以我必须在[Key]属性下面添加一个外键注释,如下所示:

texture swizzling

Entity Framework 6: one-to-one relationship with inheritance