3

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.

1
  • I have updated the question to include the query that is thowing the exception. Commented Dec 11, 2015 at 2:07

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for trying to help me. I added this annotation to all three classes but I am still getting the same error. I am unsure as to what could be causing this.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.