views:

57

answers:

2

I want to perform a generic insert in entity framework. This is what I wrote -

static public void Insert<T>(MyEntities DataContext, T obj) where T : class
        {
            try
            {
                DataContext.AddObject(DataContext,obj);
                DataContext.SaveChanges();
            }
            catch (Exception e)
            {
                throw new Exception("Problems adding object" + e);
            }
        }

But as you can see the AddObject method is not what i want...it gives exception as it expects an enitysetname I want to pass in the object and then add that object to my database. But I cannot do AddtoObjectName() as I dont know the object. Can anyone point me in the right direction here..

+1  A: 

The problem is that the Entity Framework allows for the possibility of multiple sets that use the same type, so it must have a type name in order to work. If you know that you won't be using multiple sets with the same type, you can follow a naming convention that makes it possible to construct the name from the type.

We had the same issue, and we originally decided to name each entity set [Type]Set (e.g. FormSet, ActivitySet).

With the advent of .NET 4.0, Microsoft has exposed the API they use for pluralizing entity sets in the EF tool in Visual Studio, so we're looking at maybe sticking with the default plurals, and using that tool to figure out what the default name is (e.g. Forms, Activities).

using System.Data.Entity.Design.PluralizationServices;
...
internal static readonly PluralizationService PluralizationService =
        PluralizationService.CreateService(CultureInfo.CurrentCulture);
...
static public void Insert<T>(MyEntities DataContext, T obj) where T : class
{
    try
    {
        string setName = PluralizationService.Pluralize(typeof(T).Name);
        DataContext.AddObject(setName,obj);
        DataContext.SaveChanges();
    }
    catch (Exception e)
    {
        throw new Exception("Problems adding object" + e);
    }
}
StriplingWarrior
EntityCommon.PluralizationService.Pluralize where can i find this library? I am using .net 4.0
Misnomer
With EF 4 there's an even better solution: `ObjectSet`.
Craig Stuntz
@VJ: My mistake. EntityCommon was our class. See my updated answer.@Craig Stuntz: If I'm understanding correctly, the purpose of this method is to be generic to the point that you never have to specify which ObjectSet you're trying to insert the objects into. ObjectSet is better for one-off methods because it provides type safety, but I don't know how you'd make it work in this case.
StriplingWarrior
The source code is in my answer. It's type-safe and it works.
Craig Stuntz
+3  A: 

In EF 4 you can do:

var os = DataContext.CreateObjectSet<T>();
os.AddObject(obj);
DataContext.SaveChanges();

And please remove the stack-eating try/catch.

Craig Stuntz
Thanks...it worked...just a question you suggesting i should remove try/catch but then it would be no error handling?...I am sorry but i always use it...is there a better way to have error handling?
Misnomer
You don't have error handling with your existing code. You have error obfuscation.
Craig Stuntz
Thank you for providing the source. This will be most useful.
StriplingWarrior
@VJ: Craig's point is that you're throwing an exception anyway, which is the right thing to do from a fail-fast perspective. If you want to log the error, then you can catch it, log it, and then throw the same exception instead of a new one. If you feel that your exception needs to contain special information that the original exception wouldn't have had, go ahead and create a new exception with that additional information, but include the original exception as an InnerException.
StriplingWarrior