Import data from other DB with other properties names

So I got 2 databases, 1 SQLite file, which is updated every week (so I ll need to run this update once a week). And a SQLserver (but now using SQLfile just for localtesting etc). However I am looking for a easy way to copy all the data from the SQLite file to the SQLserver, there like 10 classes, and they exist in both databses, except the sqlite has no capitals at the start while the sqlserver does.

there is a person table in the sqlite with id, name and lastname. While on the SQLserver the table is called Person (with a capital) and the columns are Id Name and Lastname. The SQlite file is provided by a 3th party and I can t decide whatever they do. The SQLserver is made with Code First EF. (And I prefere to CamelCase it all).

Is there an easy way to copy all the data, I was looking for a property loop of the classes and then to lower for example.

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.
} 

I am not sure or I am going the right direction, I have googled on how to loop over properties, but no luck on finding a good way of copying the data. I also got headache from trying to fix VS 2012 to work with SQLite, but the ADO.net doesn t seem to work so I had to install VS2010 again etc lol (running both now).

Hope someone has a good solution for this, if you got any more questions feel free to ask.

Edit: I forgot to mention it also has a navigation propertys. (Like PersonAbilitys). Which also need to be copied

Answers


This should done what you are trying (not tested, maybe fails):

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.
} 

However, you can improve it by pre calculating the property mappings, so you don't have to lookup it for every SQLiteDB.persons.

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);
}

You can also pick up an object mapper, e.g. AutoMapper. Then you can create custom conventions to make it work.

If you have more complex entities then you should go with AutoMapper instead of rolling your own mapping solution to save yourself time: AutoMapper Wiki

Btw the most straightforward way to do it is to create hand written mappings.


Need Your Help

Padding the content of a RichEditBox in a C# WinRT app

c# xaml windows-runtime windows-store-apps padding

I'm trying to apply padding to the top and bottom of a RichEditBox in a XAML/C# WinRT app, but it isn't producing the desired effect.

Determine if an HTML element's content overflows

javascript html css

Can I use JavaScript to check (irrespective of scrollbars) if an HTML element has overflowed its content? For example, a long div with small, fixed size, the overflow property set to visible, and no