Hi all

I'm new to the Entity Framework and trying to get my head around it but struggling somewhat.

I'm trying to do something basic like the below but it bombs out with a 'Null reference exception - Object reference not set to an instance of an object." when I try and do em.AddToCompanies(c); I'm just not able to debug it to get a decent error. I have manually checked there are no non-nullable fields that need filling in. tried catching an exception but I just get the same.

Can anyone tell me how I debug the Entity framework? I have tried on VS2008 and vs2010 with zero luck.

Company c = new Company();
            c.Code = "MyCode";
            c.FullName = "test co";
            c.IsActive = true;
            c.Language = "ENG";

            em.AddToCompanies(c);
            em.SaveChanges();

Use an if statement:

if (em==!null)
{
    em.AddToCompanies(c);   
    em.SaveChanges();
}

You have indicated you are getting a "null reference" error, and you have provided a snippet that does not reveal the cause of the error; unless it is because "em" has not been instantiated. I suggest you zip up the project and attach it with data. If you cannot do that, at the very least you need to indicate which line causes the error so we can direct you to the possible cause.

EDIT: Just saw ddanbe replied with the same suspicion related to "em"...

Sorry I forgot to mark as solved. My model got screwed up due to loads of refactoring and using vs2010 beta. I trashed the model and started again which fixed everything and my code worked.
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.