Reputation: 13
I have three tables with same structure. I am using entity framework. I want to create generic function that accepts only that three class types. but i couldn't give more than one type in type parameter. Is there any way? or i want to add only base class, how to create base class because they are generated from entities?
Upvotes: 1
Views: 1973
Reputation:
The easiest way would probably to not use a base class, but to use an interface. Let's suppose the common property is string Name
, then you could do
interface IEntityWithName
{
string Name { get; set; }
}
// make sure this is in the same namespace and has the same name as the generated class
partial class YourEntity1 : IEntityWithName
{
}
// ditto
partial class YourEntity2 : IEntityWithName
{
}
public void DoSomething<T>(T entity)
// if you have no common base class
where entity : class, IEntityWithName
// or if you do have a common base class
where entity : EntityObject, IEntityWithName
{
MessageBox.Show(entity.Name);
}
What exactly is possible depends on how your entity classes are generated, and on what you want to do in your procedure. If you cannot figure out how to adapt this to your situation, could you give more information about what you're trying to do?
Upvotes: 5