Convert.ChangeType() on EntityObject

I'm working on MySQL using .Net Connector 6.3.6 and created Entity models on VS 2010. I'm planning to write a generic method that would add an EntityObject to its corresponding table. Here is how it looks:

    public void AddToTable(ObjectContext dataContext, string tableName, EntityObject tableObj)
    {
                try
                {                        
                    Type type = dataContext.GetType();
                    string methodName = "AddTo" + tableName;
                    MethodInfo methodInfo = type.GetMethod(methodName);
                    PropertyInfo propInfo = dataContext.GetType().GetProperty(tableName);
                    Object[] parameters = new Object[] { Convert.ChangeType(tableObj, propInfo.PropertyType) };
                    methodInfo.Invoke(dataContext, parameters);
                }
                catch (Exception e)
                {

                  edit://gonna handle it appropriately here!
                }  
     }

ObjectContext will be the actual ObjectContext class. But I'm getting exception saying "object must implement IConvertible" when I use Covert.ChangeType() on an EntityObject. How to overcome this problem? Edit: FYI, my main intention is to make write a method which is as generic as possible so that no casting to a particular table type would be required.

Thanks, Alerter

Answers


You're reinventing the wheel.

 public void AddToTable<TEntity>(ObjectContext dataContext, TEntity tableObj)
 {
     dataContext.CreateObjectSet<TEntity>().AddObject(tableObj);
 }

And please don't eat exceptions.


Followed the following generalized repository pattern: [link]http://www.codeproject.com/Articles/37155/Implementing-Repository-Pattern-With-Entity-Framew[link] It is very intuitive and fits my requirement :)


Need Your Help

How do websites find out which browser is visiting them

c# asp.net browser user-agent

How do websites find out which browser is visiting them

Dynamic UIPickerView with multiple TextField

ios uipickerview

I am using UIPickerView with UITextField. It is working when I use only one UITextField.