2

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

Alerter
  • 249
  • 4
  • 13

2 Answers2

2

You're reinventing the wheel.

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

And please don't eat exceptions.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • Thanks Craig for the answer. But how do you make this method generic? If my understanding is correct we need to write AddToTable for each table, correct? Please explain through an example as I'm a noob at EF. PS: Don't worry about catching exceptions. That was just an example ;) – Alerter Mar 02 '11 at 16:32
  • My answer is already generic. It's the only one you need. Actually, you don't really need it at all; I'm just wrapping a standard EF feature. – Craig Stuntz Mar 02 '11 at 16:33
  • Thanks for the super fast reply. I'm still not getting your answer tbh!! Let's say I've table A, table B If I want to insert a "row" - EntityObject to table A then I should write something like this: public void AddToTable(ObjectContext dataContext, tableA tableObj) { dataContext.CreateObjectSet().AddObject(tableObj); } And similarly for table B: public void AddToTable(ObjectContext dataContext, tableB tableObj) { dataContext.CreateObjectSet().AddObject(tableObj); } But this wouldn't be generic one would it? – Alerter Mar 02 '11 at 16:36
  • No. The answer I gave already works for both A and B. You just call `AddToTable(context, objectA)` or `AddToTable(context, objectB)`. – Craig Stuntz Mar 02 '11 at 16:43
  • ya but in this code we need to cast to its corresponding type correct? dataContext.CreateObjectSet().AddObject(tableObj); TEntity = TableA or TableB! I get "Mapping and metadata information could not be found for EntityType 'System.Data.Objects.DataClasses.EntityObject'." exception when used on EntityObject without casting!! – Alerter Mar 02 '11 at 16:49
  • You're not calling it with a strongly-typed reference, then. At some point in your code, you do need to specify the type *somewhere.* So yes, casting would work. – Craig Stuntz Mar 02 '11 at 16:54
  • then I need a switch statement to cast. I don't want it. – Alerter Mar 02 '11 at 16:59
  • Then you should ask a question that explicitly specifies your use case instead of expecting volunteers to guess what it is. – Craig Stuntz Mar 02 '11 at 17:11
  • sorry Craig for that mistake. This is my first question and I'm really thankful for your support. I need this method to be generic as much as possible so that it would support all future tables too. – Alerter Mar 02 '11 at 17:15
  • 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 :) – Alerter Jun 13 '12 at 15:00
0

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 :)

Alerter
  • 249
  • 4
  • 13