.Net Framework 4.5 AddObject() does not appear

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 16:35:32

问题


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?


回答1:


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.

Cheers!!




回答2:


AddObject exists on each entity inside your container.

Context.TableName.AddObject(New TableElement...)


来源:https://stackoverflow.com/questions/18523512/net-framework-4-5-addobject-does-not-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!