Help in using AttachObjectGraph
I am using AttachObjectGraph
and SaveChanges
to save a record in my sql database. When I edit the object that I want to save, everything works fine. But when I create a new instance of the object and try to save it I get an Object reference not set to an instance of an object.
exception on AttachObjectGraph. I am sure that I am giving values to all the properties of the object and not leaving anything blank or null. What is happening?
Sample code:
class SimpleOBJ
{
public string userName;
public string password;
}
public void insertOBJ(SimpleOBJ S)
{
if (string.IsNullOrEmpty(S.password))
S.password = "test";
db.AttachOb开发者_如何学运维jectGraph(S);
db.SaveChanges();
}
the object is more complex than that i posted, it contains relations with other entities. i was passing the object throught wcf to insertOBJ. The exception occured when i changed a property that is a relation inside insertOBJ (similar to S.password, but consider password as being a relation to another entity). so the object is already passed through the wcf, then a relation property is changed , and then attachobjectgraph is called, this attachobjectgraph takes the object as its passed from wcf and not as changed in the insertOBJ function. hence the exception occures if this property was null.
therefore the solution is to change this property in the webpage, before passing it to WCF.
精彩评论