6

I am looking for a solution in the following problem:

I have many tables that differentiate from one another in few/none columns (Why this happens is not the case as I have not designed said database nor can I change it).

Example: Table User: Columns{name,surname,age} Table OldUser: Columns(name,surname,age,lastDateSeen} etc

Is there any way to indicate to the EntityFramework 4.0 in Visual Studio to extend a base class consisting of _name,_surname,_age fields and their corresponding properties so that I can use that class for batch jobs in the code?

My current solution is to make this class and use converters to pass the values from the persistent objects its' objects. It works but it is not elegant and I don't like it.

I come from a java/hibernate environment where this is basic functionality.

(for future refernce can the same thing be done if I want the classes to implement an interface?)

Thanks in advance.

4
  • Is this actually an entity framework question? I've never heard of Entity Manager. Fortunately, the particular ORM technology doesn't affect the answer to your question. Commented Dec 28, 2012 at 2:15
  • You are absolutely right, I meant EntityFramework Commented Dec 28, 2012 at 18:21
  • So did my answer solve your question? 'Cause an approved answer would sure be appreciated! Thanks! Commented Jan 3, 2013 at 23:15
  • While informative, and being a really good answer, the question was not how to write an interface and implement it for as you said this will be ovewritten next time you update the DB in the Entity framework. All I am asking for is a way to indicate to the mappings that they need to take their information from specific base classes, or to indicate that these tables need not be updated. Commented Jan 6, 2013 at 16:07

1 Answer 1

1

Since your RDBMS (at least SQL Server 2008 and older) doesn't allow for table inheritance, I would recommend that you do not use inheritance in the DB model in C#. This is especially recommended when you cannot control the design of the tables.

Instead use an interface if you actually have clients of those classes who will benefit from the abstraction, but not being able to control the design of the DB makes this less valuable because the DB designer could change the tables thereby making your EF classes no longer implement the interface:

public interface IUser {
  public string Name { get; }
  // etc...
}

public class User : IUser {
  public string Name { get; set; }
  // etc...
}

public class OldUser : IUser {
  public string Name { get; set; }
  // rest of IUser
  public DateTime? LastSeenOn { get; set; }
}
Sign up to request clarification or add additional context in comments.

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.