1

I have entity instance(north)

NorthdatabaseEntity north = new NorthdatabaseEntity()

The problem is that i don't have access to ObjectContext methods. I tried to call them with object "north" but i didn't found the method "AddObject" or any other from ObjectContext. I thought that any entity object has all methods from ObjectContext class.

Where is the problem here?

1
  • Only the context object is responsible for managing entities. Commented Jan 21, 2013 at 9:20

1 Answer 1

1

ObjectContext and entities are different objects; you may use ObjectContext to add/modify/delete objects, but entities does not have direct access to ObjectContext.

You need to create an instance of ObjectContext and perform your operations on itself, not on entities.

Sign up to request clarification or add additional context in comments.

8 Comments

I have example in VS2012 with following code static int SelectCustomersCount() { NorthwindEntities northwindEntities = new NorthwindEntities(); string nativeSQLQuery = "SELECT count(*) FROM dbo.Customers"; var queryResult = northwindEntities.ExecuteStoreQuery<int>(nativeSQLQuery); int customersCount = queryResult.FirstOrDefault(); return customersCount; } and here it works but when I try to create my own object i cannot see the method "ExecuteStoreQuery()" or "AddObject()" method with my entity object. Why in this example work ?
But why NorthwindEntities object in the example can see methods like "ExecuteStoreQuery()", "AddObject()" and my object cannot ? They are from the same type (NorthwindEntities).
can you put the cursor on NorthdatabaseEntity and press F12. What is the base type of NorthdatabaseEntity?
When I pressed F12 I saw that the class "NorthwindEntities" from the example is "public partial class NorthwindEntities : ObjectContext". My object is from the same class "NorthwindEntities" but whe n I press F12 I saw "public partial class NorthwindEntities : DbContext". Why when I create my own object the class inherit DBContext, the other from example inherit ObjectContext. This is the problem i think but why VS make them inherit from different classes ?
Yes.But I think the problem here is that my class inherit DBContext and with F12 i changed from "DBContext" to "ObjectContext" and now it work as i want to work. But is it a good practice to change the classes structure with F12 ? What do you think ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.