2012-09-10 4 views
0

그래서 저는 매주 업데이트되는 2 개의 데이터베이스와 1 개의 SQLite 파일을 가지고 있습니다. 따라서 일주일에 한 번이 업데이트를 실행해야합니다. 그리고 SQL 서버 (하지만 지금은 localtesting만을위한 SQLfile을 사용합니다). 그러나 나는 SQLite 파일에서 SQL 서버로 모든 데이터를 복사하는 쉬운 방법을 찾고 있는데 거기에는 10 개의 클래스가 있으며 sqlserver가 시작될 때 sqlite에는 대문자가 없다는 것을 제외하면 두 databses에 모두 존재합니다.다른 속성 이름을 가진 다른 DB에서 데이터 가져 오기

id, name 및 성을 가진 sqlite에 person 테이블이 있습니다. SQLserver에서 테이블은 Person (대문자 포함)이라고하고 열은 Id Name 및 Lastname입니다. SQlite 파일은 제 3 자에 의해 제공되며 어떤 일을하든 결정할 수 없습니다. SQL Server는 Code First EF로 만들어졌습니다. (그리고 나는 CamelCase에 그것을 모두 좋아한다.)

모든 데이터를 쉽게 복사 할 수 있습니까? 예를 들어 클래스의 속성 루프를 찾은 다음 예를 들어 낮추려했습니다.

ForEach(var person in SQLiteDB.persons) 
{ 
    Person thenewperson = new Person(); 
    foreach (var prop in person.GetType().GetProperties()) 
    { 
     // Here I get stuck I want something like 
     if(thenewperson.Haspropertie.ToLower(prop.GetName.ToLower) // This is incorrect but I think you ll understand if we do a ToLower then we can get it to work. Since only capitals are the things that are different. 
     { 
      //Then we copy the data to "thenewperson" 
     } 

    } 
    SQlserverDB.Persons.AddorUpdate(thenewperson) // Not totaly correct but you get the point. 
} 

나는 확실하지 않다거나 내가 올바른 방향을 가고있다가, 나는에 봤 얼마나 특성을 통해 루프하지만, 데이터를 복사하는 좋은 방법을 찾는 행운을. 또한 SQLite와 함께 작동하도록 VS 2012를 수정하려고했지만 ADO.net이 작동하지 않는 것처럼 보였습니다. 그래서 VS2010을 다시 설치해야했습니다. LOL (지금 실행 중입니다).

더 많은 질문이 있으면 언제든지 누군가에게 좋은 해결책이 있기를 바랍니다.

편집 : 탐색 속성이 있습니다. (PersonAbility와 마찬가지). 그렇게하지 않도록

foreach(var person in SQLiteDB.persons) 
{ 
    Person thenewperson = new Person(); 
    foreach (var prop in person.GetType().GetProperties()) 
    { 
     var newPersonProp = thenewperson.GetType().GetProperties() 
                .FirstOrDefault(p => p.Name.ToLower() == prop.Name.ToLower()); 

     if (newPersonProp != null) 
     { 
      newPersonProp.SetValue(thenewperson, prop.GetValue(person, null), null); 
     } 
    } 
    SQlserverDB.Persons.AddorUpdate(thenewperson); // Not totaly correct but you get the point. 
}

을하지만, 당신은 속성 매핑을 계산 사전하여 향상시킬 수도

이것은 당신이 (아마도 실패, 테스트하지)하려고하는 수행해야

답변

1

을 복사해야하는 모든 SQLiteDB.person에 대해 조회해야합니다.

var propertyMapping = new Dictionary<PropertyInfo, PropertyInfo>(); 
var efPersonType = typeof(Person); 
var sqlitePersonType = typeof(SQLLitePerson); 

foreach (var prop in sqlitePersonType.GetProperties()) 
{ 
    var efProp = efPersonType.GetProperties() 
          .FirstOrDefault(p => p.Name.ToLower() == prop.Name.ToLower()); 
    if (efProp != null) 
    { 
     propertyMapping.Add(prop, efProp); 
    } 
} 

foreach (var person in SQLiteDB.persons) 
{ 
    Person thenewperson = new Person(); 
    foreach (var i in propertyMapping) 
    { 
     i.Value.SetValue(thenewperson, i.Key.GetValue(person, null), null); 
    } 
    SQlserverDB.Persons.AddorUpdate(thenewperson); 
} 

당신은 또한 객체 매퍼을 선택할 수 있습니다, 예를 들어, AutoMapper. 그런 다음 사용자 정의 규칙을 만들어 작동시킬 수 있습니다.

당신은 다음 AutoMapper와 함께 가야하는 대신 자신의 매핑 솔루션을 압연하는 것은 자신에게 시간을 절약하기 위해 더 복잡한 실체가있는 경우 : AutoMapper Wiki

가 손으로 쓴 매핑을 만드는 것입니다 할 Btw는 가장 간단한 방법입니다.

+0

답장을 보내 주셔서 감사합니다.하지만 컬렉션에 도착하기 전까지는 훌륭하게 진행되고 있다고 생각합니다. 'System.Data.Objects.DataClasses.EntityCollection'1 [DataImport.personAbility]'유형의 개체를 'System.Data.Objects.DataClasses.EntityCollection'1 형식으로 변환 할 수 없습니다. [DataImport.PersonAbility – Maximc

+0

클래스에 탐색 속성이 있습니다. 그것은 그 이야기를 복잡하게 만든다. –

+0

죄송합니다. 죄송합니다. 매우 복잡합니다. /? – Maximc

관련 문제