Inheritance in .Net EntityFramework 4.0 for similar database tables

I am looking for a solution in the following task:

I have many tables that differ from each other in few / non-columns (why this does not happen because I did not design the indicated database and cannot change it).

Example: Table user: columns {first name, last name, age} OldUser table: Columns (first name, last name, age, lastDateSeen}, etc.

Is there a way to tell 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 this class for batch jobs in code?

My current solution is to make this class and use converters to pass values โ€‹โ€‹from the constant objects of its โ€œobjectsโ€. It works, but itโ€™s not elegant, and I donโ€™t like it.

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

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

Thanks in advance.

+6
source share
1 answer

Since your RDBMS (at least SQL Server 2008 and later) does not allow table inheritance, I would recommend that you not use inheritance in the DB model in C #. This is especially recommended if you cannot control the design of the tables.

Instead, use the interface if you really have clients of those classes that will benefit from the abstraction, but the inability to control the database design makes it less valuable because the database designer can modify 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; } } 
+1
source

All Articles