[英]Entity Framework same entities
对于这个愚蠢的问题,我真的感到很抱歉,但是我有一个问题,不知道如何解决。 我有一个带有一些具有相同结构的表的数据库。 我首先在该数据库上使用了Entity Framework数据库。 现在,我有一些相同的实体。 例如,
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;}
}
...
我需要使用WCF传输此实体。 所以我创建了一个像这样的实体的数据合同。 现在,我想创建像下面这样的特定更新方法:
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
}
}
有什么方法可以为具有这种结构的所有实体创建一个方法?
感谢您的任何建议。
介绍一个接口:
public interface ICommonEntity
{
int ID {get;set;}
string Name {get;set;}
bool Flag {get;set;}
}
将其应用于您的实体:
public partial class Entity1 : ICommonEntity {}
public partial class Entity2 : ICommonEntity {}
使您的“通过ID从数据库获取Entity的某种方法”返回该接口:
public ICommonEntity GetFromDatabase(...);
然后,对于所有实体类型,您只需要一种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.