1

I've been getting this error in my project. Been stuck for a month now. The problem is, I'm working on a enhancement of an existing project and the domain layer is really complex with more than 50 tables, so I'm unable to pinpoint the root cause.

The funny thing is, if the project where all the domain classes resides are within the main solution, the error occurs. But when I copy the project out and build it, copy the dll to the web application bin folder, it works without error. Exactly the same code. Other team members also do not experience this error.

Also, when I get the original source code before our development from TFS, the same error occurs. We have been able to do development without any error at least in 2 weeks in the beginning. So is it possible the SQL DB causing the error?

So I'm wondering if it's an EF bug. Any pointers to help me debug or find out the culprit domain class?

Here's the error screenshot.

http://imageshack.us/photo/my-images/10/8a1w.jpg/

--UPDATE-- The error is on the context.Database.Initialize when the web app is started.

Managed to find the entity causing the problem. a. It's TPT + TPH b. 2 tables having the same foreign key property name, in my example below, it's the WorkerBenefit property.

It's been reported here: http://entityframework.codeplex.com/workitem/677. And it states Release: 6.0.0, means I have to use EF6?

[Table("Person")]
public abstract class Person
{
    public int Id { get; set; }
    public string PersonNumber { get; set; }
}

[Table("FullTimeWorker")]
public class FullTimeWorker : Person
{
    public string FullTimeWorkerProperty { get; set; }

    [ForeignKey("WorkerBenefit")]
    public int WorkerBenefitId { get; set; }

    [ForeignKey("WorkerBenefitId")]
    public virtual WorkerBenefit WorkerBenefit { get; set; }
}

[Table("PartTimeWorker")]
public class PartTimeWorker : Person
{
    public string PartTimeWorkerProperty { get; set; }

    [ForeignKey("WorkerBenefit")]
    public int WorkerBenefitId { get; set; }

    [ForeignKey("WorkerBenefitId")]
    public virtual WorkerBenefit WorkerBenefit { get; set; }
}

public class MorningShiftWorker : PartTimeWorker 
{
    public string MorningShiftWorkerProperty { get; set; }
}

public class EveningShiftWorker : PartTimeWorker 
{
    public string EveningShiftWorkerProperty { get; set; }
}
vincent007
  • 41
  • 8
  • What query produces the error? Can you show us the POCO classes that are involved? – Dabblernl Nov 03 '13 at 00:12
  • It's not a query, it's at the context.Initialize method. – vincent007 Nov 04 '13 at 07:35
  • 1
    Can you confirm that the probem goed away with EF6? Now that you knwo the cause you should be able to reproduce it on a smaall test context. – Dabblernl Nov 04 '13 at 07:55
  • Hi Dabblernl, the sample I posted is the small test context. Actually I will not be able to use EF6 as it is an existing web app in production. Like I mentioned in my post, copying out the project and build the dll individually, then copy it to the web app bin folder solves the problem. But this is not a long term solution. – vincent007 Nov 04 '13 at 08:24
  • I have tested, it works with EF6, EF5 still has the same problem. – vincent007 Nov 05 '13 at 03:12
  • Still looking for a workaround for EF 4.3.1. Any help would be appreciated. – vincent007 Nov 07 '13 at 07:15

1 Answers1

0

Maybe I might not fully understand your code, but instead of:

[Table("FullTimeWorker")]
public class FullTimeWorker : Person
{
    public string FullTimeWorkerProperty { get; set; }

    [ForeignKey("WorkerBenefit")]
    public int WorkerBenefitId { get; set; }

    [ForeignKey("WorkerBenefitId")]
    public virtual WorkerBenefit WorkerBenefit { get; set; }
}

[Table("PartTimeWorker")]
public class PartTimeWorker : Person
{
    public string PartTimeWorkerProperty { get; set; }

    [ForeignKey("WorkerBenefit")]
    public int WorkerBenefitId { get; set; }

    [ForeignKey("WorkerBenefitId")]
    public virtual WorkerBenefit WorkerBenefit { get; set; }
}

Shouldn't it be:

[Table("FullTimeWorker")]
public class FullTimeWorker : Person
{
    public string FullTimeWorkerProperty { get; set; }

    [ForeignKey("WorkerBenefit")]
    public int WorkerBenefitId { get; set; }

    public virtual WorkerBenefit WorkerBenefit { get; set; }
}

[Table("PartTimeWorker")]
public class PartTimeWorker : Person
{
    public string PartTimeWorkerProperty { get; set; }

    [ForeignKey("WorkerBenefit")]
    public int WorkerBenefitId { get; set; }

    public virtual WorkerBenefit WorkerBenefit { get; set; }
}

[Table("WorkerBenefit")]
public class WorkerBenefit : Person
{
    public int WorkerBenefitId { get; set; }

    public string benefit { get; set; }
}

This may help -- assuming the issue is related to the foreign keys like I think: How Should I Declare Foreign Key Relationships Using Code First Entity Framework (4.1) in MVC3?

If not, this is likely a problem with the models vs. the database. Had a similiar issue and, if you comment out the DbSets in your main database entity that point to these tables, then uncomment one by one, you'll find the one it's complaining about.

Community
  • 1
  • 1
vapcguy
  • 7,097
  • 1
  • 56
  • 52