Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Framework 4.5 AddObject() does not appear

I have a class that I want to make Insert, Update, Delete operations in it.

// Constructor.
public BaseManager()
{
    // Disable lazy loading.
    this.Context.Configuration.LazyLoadingEnabled = false;
}

public DBEntities Context = new DBEntities();

In this class, I can't use AddObject() extension method on Context variable. AddObject() method does not appear typing after Context.

Here are my imported namespaces:

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;

I was able to use it in .Net Framework 3.5 but not working on .Net Framework 4.5

What I've doing wrong?

UPDATE:

Importing using System.Data.Entity; or using System.Data.Objects; not working.

Here is the method I want to use: http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx

UPDATE AGAIN:

I realized that my DBEntities derives from DbContext in .Net Framework 4.5 but it was deriving from ObjectContext in .Net Framework 3.5, so I was able to use AddObject() method.

Original entities class:

public partial class DBEntities : DbContext
    {
        // ...
    }

I want to do like this:

public partial class DBEntities : ObjectContext
    {
        // ...
    }

If I change base class from DbContext to ObjectContext does it constitute any problem?

like image 564
JustWork Avatar asked Aug 30 '13 01:08

JustWork


2 Answers

Expand YourModel.edmx file. you can see 4 files there.Delete the files with .tt extension.. Then double click on YourModel.edmx file. In YourModel.edmx diagram window right click and chose Properties.

In the properties window set Code Generation Strategy None to Default.

Screenshot

Cheers!!

like image 51
Aslam Jiffry Avatar answered Sep 28 '22 18:09

Aslam Jiffry


AddObject exists on each entity inside your container.

Context.TableName.AddObject(New TableElement...)
like image 41
OneFineDay Avatar answered Sep 28 '22 18:09

OneFineDay