Geeks With Blogs

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites


Anthony Trudeau

It's another week and another restriction imposed by the Entity Framework (EF). Don't get me wrong. I like EF, but I don't like how it restricts you in different ways. At this point you may be asking yourself the question: how can you have more than one table with the same name?

The answer is to have tables in different schemas. I do this to partition the data based on the area of concern. It allows security to be assigned conveniently. A lot of people don't use schemas. I love them. But this article isn't about schemas.

In the situation I have two tables:

  • Contact.Person
  • Employee.Person

The first contains the basic, more public information such as the name. The second contains mostly HR specific information. I then mapped these tables to two classes. I stuck to a Table per Class (TPC) mapping, because of problems I've had in the past implementing inheritance with EF. The following code gives you the basic contents of the classes.

[Table("Person", Schema = "Employee")]
public class Employee {
   ...
   public int PersonId { get; set; }

   [ForeignKey("PersonId")]
   public virtual Person Person { get; set; }
}

[Table("Person", Schema = "Contact")]
public class Person {
   [Key]
   public int Id { get; set; }
   ...
}

This seemingly simple scenario just doesn't work. The problem occurs when you try to add a Person to the DbContext. You get an InvalidOperationException with the following text:

The entity types 'Employee' and 'Person' cannot share table 'People' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them..

This is interesting for a couple of reasons. First, there is no People table in my database. Second, I have used the SetInitializer method to stop a database from being created, so it shouldn't be thinking about new tables.

The solution to my problem was to change the name of my Employee.Person table. I decided to name it Employee.Employee. It's not ideal, but it gets me past the EF limitation. I hope that this article will help someone else that has the same problem.

Posted on Tuesday, July 2, 2013 9:17 PM .NET | Back to top


Comments on this post: EF Doesn't Like Same Named Tables

# re: EF Doesn't Like Same Named Tables
Requesting Gravatar...
Very good stuff with good ideas and concepts, lots of great information and inspiration, both of which we all need. http://www.replicahaandbags.co.uk
Left by Iaran on Sep 09, 2013 3:10 AM

# re: EF Doesn't Like Same Named Tables
Requesting Gravatar...
Hello

Your issue is related to below, which has been raised with the EF team -
https://entityframework.codeplex.com/workitem/1641

regards
Left by iRahat on Sep 18, 2013 1:58 AM

# re: EF Doesn't Like Same Named Tables
Requesting Gravatar...
I added my vote.
Left by Anthony Trudeau on Sep 18, 2013 7:26 AM

Your comment:
 (will show your gravatar)


Copyright © Anthony Trudeau | Powered by: GeeksWithBlogs.net