本文介绍了找不到InsertOnSubmit()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Entity Framework的新手,我认为这里有些误会.我试图在表中插入一行,在所有我发现代码示例的地方,它们都调用方法InsertOnSubmit(),但是问题是我在任何地方都找不到方法InsertOnSubmit或SubmitChanges.

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.

错误告诉我:System.Data.Object.ObjectSet不包含InsertOnSubmit的定义,...

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

我做错了什么?

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

在FW 4.0上使用Visual Studio 2010

Using Visual Studio 2010 on FW 4.0

推荐答案

InsertOnSubmit是Linq-to-SQL方法,不在实体框架中.

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

但是,由于我们的项目是从Linq到SQL的转换,因此我们提供了一些扩展方法,这些方法可能会有所帮助:

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);
    }
}

EntityHelper MyExtensions 开源.

Where EntityHelper is as per the MyExtensions open source library.

这篇关于找不到InsertOnSubmit()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 12:27