Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:01:29+00:00 2026-06-17T14:01:29+00:00

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

  • 0

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 ObjectSet
p.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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:01:31+00:00Added an answer on June 17, 2026 at 2:01 pm

    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;  
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this error (Upgrading old nbandroid project projectName FAILED. See log file
I have upgraded an old project from Delphi 7 to Delphi XE. The project
I've upgraded a project from Rails 3.1 to Rails 3.2, and now, while in
I have started a project using Kendo UI for MVC BETA and now I
I upgraded my entity framework 4.3 database first project to the new entity framework
I upgraded a project to Entity Framework 4.3 and enabled migrations on the project.
I've just received an assignment to upgrade an old Delphi 3 project that I
I have successfully upgraded an MFC application which was compiled with an old version
so I upgraded to python 3.1.1 from 2.6 and i ran an old program
I recently upgraded from Vista/32 to Win7/64. On my old machine, everything was working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.