本文介绍了ObjectContext.AddObject(的entityName,实体)上的DbContext等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级旧项目开始于 EF4 但现在我迁移到 EF5

这是旧的code:

 保护无效SaveEntity< T>(T实体)
{
        使用(DocsManagerContainer上下文=新DocsManagerContainer())
                {
                    字符串的EntityType = typeof运算(T)的ToString();
                    。GetLogger()的LogMessage(保存+的EntityType +开始,LogLevel.Info);
                    DbTransaction交易= NULL;
                    尝试
                    {
                        context.Connection.Open();
                        交易= context.Connection.BeginTransaction();
                        context.AddObject(typeof运算(T)+ .Name点的s,实体);
                        器transaction.commit();
                        context.SaveChanges();
                    }
                    赶上(例外五)
                    {
                        。GetLogger()的LogMessage(保存+的EntityType +抛出的错误:,即LogLevel.Error);
                        扔ê;
                    }
                    最后
                    {
                        context.Connection.Close();
                        交易= NULL;
                    }
                    。GetLogger()的LogMessage(保存+的EntityType +结束,LogLevel.Info);
                }
    }
 

我几乎升级的所有的code除外: context.AddObject(typeof运算(T).Name点+S,实体); ,但这是不支持了。
如何升级呢?

P.S。我不希望使用通用的code,不要用开关添加相应的对象,纠正对象集附:错误,如果我使用。设置()添加(实体)是:

 错误2类型'T'必须是引用类型,以便在泛型类型或方法System.Data.Entity.DbContext.Set&LT将它作为参数'TEntity ; TEntity>()'D:\工作\ DocsManager \干线\ DocsManagerDataMapper \ EF4Factory \ BaseEF4Factory.cs 64 21 DocsManagerDataMapper
 

解决方案

使用的DbContext,您可以使用 context.Set< T>()添加(实体);

例如: context.Set<使用者>()等同于 context.Users context.Set<使用者>()添加(MYUSER)等同于 context.Users.Add(MYUSER)

您想要的东西更接近这样的:

 保护无效SaveEntity< T>(T实体)
    其中T:类
{
    使用(DocsManagerContainer上下文=新DocsManagerContainer())
    {
        DbTransaction交易= NULL;
        尝试
        {
            context.Connection.Open();
            交易= context.Connection.BeginTransaction();
            context.Set< T>()添加(实体)。
            器transaction.commit();
            context.SaveChanges();
        }
        最后
        {
            context.Connection.Close();
                交易= NULL;
        }
    }
}
 

I upgraded an old project started in ef4 but now I migrated it to ef5.

This is the old code:

protected void SaveEntity<T>(T entity)
{
        using (DocsManagerContainer context = new DocsManagerContainer())
                {
                    string entityType = typeof(T).ToString();
                    GetLogger().LogMessage("Save " + entityType + " started", LogLevel.Info);
                    DbTransaction transaction = null;
                    try
                    {
                        context.Connection.Open();
                        transaction = context.Connection.BeginTransaction();
                        context.AddObject(typeof(T).Name + "s", entity);
                        transaction.Commit();
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        GetLogger().LogMessage("Save " + entityType + " thrown error :", e, LogLevel.Error);
                        throw e;
                    }
                    finally
                    {
                        context.Connection.Close();
                        transaction = null;
                    }
                    GetLogger().LogMessage("Save " + entityType + " ended", LogLevel.Info);
                }
    }

I've upgraded almost all of the code except : context.AddObject(typeof(T).Name + "s", entity);, but this isn't supported anymore.
How can I upgrade this ?

p.s. I do want to use generic code, not to use switches to add the corresponding object to correct ObjectSetp.s. Error if I use .Set().Add(entity) is :

Error   2   The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Entity.DbContext.Set<TEntity>()' D:\work\DocsManager\trunk\DocsManagerDataMapper\EF4Factory\BaseEF4Factory.cs    64  21  DocsManagerDataMapper
解决方案

With DbContext you can use context.Set<T>().Add(entity);

Example: context.Set<User>() is equivalent to context.Users so context.Set<User>().Add(myUser) is equivalent to context.Users.Add(myUser).

You want something closer to this:

protected void SaveEntity<T>(T entity)
    where T : class
{
    using (DocsManagerContainer context = new DocsManagerContainer())
    {
        DbTransaction transaction = null;
        try
        {
            context.Connection.Open();
            transaction = context.Connection.BeginTransaction();
            context.Set<T>().Add(entity);
            transaction.Commit();
            context.SaveChanges();
        }
        finally
        {
            context.Connection.Close();
                transaction = null;
        }
    }
}

这篇关于ObjectContext.AddObject(的entityName,实体)上的DbContext等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:47