Can't find InsertOnSubmit() method

2019-02-17 18:35发布

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

The error tell me: System.Data.Object.ObjectSet do not contain the definition for InsertOnSubmit, ...

What I'm 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 on FW 4.0

4条回答
SAY GOODBYE
2楼-- · 2019-02-17 19:07

Finally found what was wrong, my Entity database was a dbmx file and not a dbml file. I do not understand why this .. but has long as it work. (Need to buy a new book I guess) – Hugo Feb 17 at 19:40

查看更多
虎瘦雄心在
3楼-- · 2019-02-17 19:09

i also have the same problem .we can insert by using Add

GMR_DEVEntities CTX;
    CTX = new GMR_DEVEntities();
    tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code = "new config code" };
    CTX.tblConfigs.Add(Config);
查看更多
狗以群分
4楼-- · 2019-02-17 19:14

InsertOnSubmit is a Linq-to-SQL method and not in the Entity Framework.

However, since our project was a conversion from Linq-to-SQL we have some extension methods that might 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);
    }
}

Where EntityHelper is as per the MyExtensions open source library.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-02-17 19:18

Hello this works for me

Entity db = new Entity();

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

                    db.TABLE_NAME.Add(table);
                    db.SaveChanges();
查看更多
登录 后发表回答