Cannot find InsertOnSubmit () method

I am new to the Entity Framework and I think there is something here that I misunderstood. I am trying to insert a row into a table, and everywhere I found a code example, they call the InsertOnSubmit () method, but the problem is that I can not find the InsertOnSubmit or SubmitChanges method anywhere.

The error tells me: System.Data.Object.ObjectSet does not contain a definition for InsertOnSubmit, ...

What am I doing wrong?

http://msdn.microsoft.com/en-us/library/bb763516.aspx

        GMR_DEVEntities CTX;
        CTX = new GMR_DEVEntities();
        tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code = "new config code" };
        CTX.tblConfigs.InsertOnSubmit(Config); // Error here

Edit: Using Visual Studio 2010 in FW 4.0

+5
source share
4 answers

InsertOnSubmit is a Linq-to-SQL method, not an Entity Framework.

However, since our project was a conversion from Linq-to-SQL, we have some extension methods that can help:

public static class ObjectContextExtensions
{
    public static void SubmitChanges(this ObjectContext context)
    {
        context.SaveChanges();
    }

    public static void InsertOnSubmit<T>(this ObjectQuery<T> table, T entity)
    {
        table.Context.AddObject(GetEntitySetName(table.Context, entity.GetType()), entity);
    }

    public static void InsertAllOnSubmit<T>(this ObjectQuery<T> table, IEnumerable<T> entities)
    {
        var entitySetName = GetEntitySetName(table.Context, typeof(T));
        foreach (var entity in entities)
        {
            table.Context.AddObject(entitySetName, entity);
        }
    }

    public static void DeleteAllOnSubmit<T>(this ObjectQuery<T> table, IEnumerable<T> entities) where T : EntityObject, new()
    {
        var entitiesList = entities.ToList();
        foreach (var entity in entitiesList)
        {
            if (null == entity.EntityKey)
            {
                SetEntityKey(table.Context, entity);
            }

            var toDelete = (T)table.Context.GetObjectByKey(entity.EntityKey);
            if (null != toDelete)
            {
                table.Context.DeleteObject(toDelete);
            }
        }
    }

    public static void SetEntityKey<TEntity>(this ObjectContext context, TEntity entity) where TEntity : EntityObject, new()
    {
        entity.EntityKey = context.CreateEntityKey(GetEntitySetName(context, entity.GetType()), entity);
    }

    public static string GetEntitySetName(this ObjectContext context, Type entityType)
    {
        return EntityHelper.GetEntitySetName(entityType, context);
    }
}

EntityHelper MyExtensions library.

+14

,

Entity db = new Entity();

TABLE_NAME table = new TABLE_NAME 
                    {
                       COLUMN1 = "TEST",
                       cOLUMN2 = "test"
                      //etc...
                    };

                    db.TABLE_NAME.Add(table);
                    db.SaveChanges();
+2

Finally, it turned out what happened, my Entity database was a dbmx file, not a dbml file. I don’t understand why this is ... but it works for a long time. (Need to buy a new book, I think) - Hugo February 17 at 19:40

+1
source

i also has the same problem. We can insert using Add

GMR_DEVEntities CTX;
    CTX = new GMR_DEVEntities();
    tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code = "new config code" };
    CTX.tblConfigs.Add(Config);
0
source

All Articles