0

I am using EF4 in VS2010 and I have the following code:

MyEntity entity = null;
entity = MyEntity.CreateMyEntity(0);
context.MyEntities.AddObject(entity);
context.AcceptAllChanges();
context.SaveChanges();

The problem is that AddObject() is not adding the object to the collection and I just can't figure out why. What happens when I step through the code is that it gets to the first property, "CityId" which is of course null and then it seems to just return from there... like it had a problem with the property being null. The only non-nullable field in my database (MSSQL) is the Id column, which is an identity column... I am quite perplexed. Please help!

Thanks in advance!

Matt

2
  • What does the MyEntity.CreateMyEntity(0); code do? I remember that I had some problems when I used POCO entities and did not use the context.CreateObject<T>() method to create the entity. Also, check the stack window in VS for suspicious EF method calls - that could give you an idea of what is going on. And again - post if the EF is configured as POCO/Code-First and what does the MyEntity.CreateMyEntity() method do. Commented Oct 5, 2011 at 5:22
  • Thank you for your reply, Jeffim. The MyEntity.CreateMyEntity() method actually shows you which properties need to be filled in (NOT NULL in db). Commented Oct 5, 2011 at 6:39

1 Answer 1

1

You are not supposed to call accept all changes method.

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

2 Comments

Thank you Akash. I can't believe it. What exactly does AcceptAllChanges() do? The documentation says: "Accepts all changes made to objects in the object context."... in what situation would I call this method?
Infect it should never be called since it is called by EF when changes are saved correctly to database. If you are trying to implement your own way of communicating with database then you should use this.

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.