[英]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.