I use EF 4.
Having this code to test the functionality of my model.
using (var context = new CamelotDB())
{
Console.WriteLine("Starting User Repository Testing{0}",Environment.NewLine);
Console.WriteLine("Creating Test User");
DomainEntity userToAdd = new User()
{
EntityName = "Test User",
EntityType = DomainEntity.eEntityType.User,
Username = "TestUser",
Password = "123",
Email = "TestUser@Test.org",
FirstName = "Test",
LastName = "User",
Phone = "123456789",
EntityParentID = null,
};
Console.WriteLine("Saving user to database...");
userToAdd = context.DomainEntities.Add(userToAdd);
context.SaveChanges();
Console.WriteLine("Fetching user from database...");
int userID = userToAdd.EntityID;
DomainEntity userToDelete = context.DomainEntities.Find(userID);
Console.WriteLine("Removing the user...");
context.DomainEntities.Remove(userToDelete);
context.SaveChanges();
}
The user adds successfully to the DB and i get back the userID.
When I try to use the Find() method in
DomainEntity userToDelete = context.DomainEntities.Find(userID);
I get the following error: value cannot be null. parameter name key
I event tried to call the follwing:
DomainEntity userToDelete = context.DomainEntities.Find(2);
Knowing that the userID 2 exists in the DB but still got the same Exception.
Got it fixed somehow… just removed the key property from the EDMX and added it again the save changes and everything went back to normal guess it was something to do with the EDMX generation…