I'm really sorry for this silly question, but I have a problem and no idea, how to solve it. I have a database with a few tables with the same structure. I've used Entity Framework database-first on this database. Now I have a few same entities. For example,
public partial class Entity1
{
public int ID {get;set;}
public string Name {get;set;}
public bool Flag {get;set;}
}
public partial class Entity2
{
public int ID {get;set;}
public string Name {get;set;}
public bool Flag {get;set;}
}
...
I need to use WCF to transport this entities. So i create a datacontract like this entities. And now I want to create specific update method like bellow:
public void update(EntityContract contract)
{
entity = //some method to get Entity from database by ID
bool needUpdate = false;
if(!contract.Name.Equals(entity.Name))
{
entity.Name = contract.Name;
needUpdate = true;
}
... use this codeblock for enother properties
if(needUpdate)
{
//update entity
}
}
Is there any way to create a one method for all entities with this structure?
Thanks for any advice.
Introduce an interface:
public interface ICommonEntity
{
int ID {get;set;}
string Name {get;set;}
bool Flag {get;set;}
}
Apply it to your entities:
public partial class Entity1 : ICommonEntity {}
public partial class Entity2 : ICommonEntity {}
Make your "some method to get Entity from database by ID" return that interface:
public ICommonEntity GetFromDatabase(...);
Then you only need one method for all entity types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.