데이터베이스 구성과 독립적으로 작동하는 또 다른 방법은 개체의 딥 클론을 수행하는 것입니다.
필자는 코드 첫 번째 EF 프로젝트에서 Automapper (https://www.nuget.org/packages/AutoMapper/)를 사용합니다. 다음은 'IonPair'라는 EF 개체 목록을 내보내는 샘플 코드입니다.
public bool ExportIonPairs(List<IonPair> ionPairList, string filePath)
{
Mapper.CreateMap<IonPair, IonPair>(); //creates the mapping
var clonedList = Mapper.Map<List<IonPair>>(ionPairList); // deep-clones the list. EF's 'DynamicProxies' are automatically ignored.
var ionPairCollection = new IonPairCollection { IonPairs = clonedList };
var serializer = new XmlSerializer(typeof(IonPairCollection));
try
{
using (var writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, ionPairCollection);
}
}
catch (Exception exception)
{
string message = string.Format(
"Trying to export to the file '{0}' but there was an error. Details: {1}",
filePath, exception.Message);
throw new IOException(message, exception);
}
return true;
}