Solo812
Solo812

Reputation: 471

Entity Framework - Database First - Invalid column name error

I have three simple classes and I am wiring up EF6 to an existing database.

Classes are as follows

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 looks like the following:

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


    }

On my existing database, I have the following table names and fields:

Applications (ApplicationID : uniqueidentifier, DateTime : datetime, CompletedZipFileURL : varchar(500))

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

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

For some reason, as soon as I attempt to do a query against the root Application POCO, I am receiving an error to the effect of "{"Invalid column name 'BusinessInfo_BusinessID'."}".

I have attempted to debug this issue checking out various SO posts but the examples/fixes don't apply to my database first scenario.

The query that is throwing the exception is:

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

I can see while debugging that the query being generated from LINQ is as follows

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]

I understand WHY I am getting the error back and that is because there is no "BusinessInfo_BusinessID" column in the Applications table.

I would greatly appreciate any help/pointers that I could get on this one.

Upvotes: 3

Views: 2617

Answers (2)

Solo812
Solo812

Reputation: 471

I have discovered that because I had a one-to-one relationship (that doesn't technically exist on the SQL server, I had to add a foreign key annotation underneath the [Key] property as noted:

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

and

http://www.entityframeworktutorial.net/entity-relationships.aspx

Upvotes: 0

Daniel Corzo
Daniel Corzo

Reputation: 1075

Check this out

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

In your query, change Where and SingleOrDefault to:

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

Hope it helps

Upvotes: 1

Related Questions