I am trying to add ProfileProperty to the ProfileProperty table using ObjectContext.AddObject .
db fields for ProfileProperties table:
ProfilePropertyID ProfilePropertyDefinitionID UserID PropertyValue
db fields for the ProfilePropertyDefinitions table:
ProfilePropertyDefinitionID PropertyName
Variables for the ProfileProperty object passed to:
ProfilePropertyID ProfilePropertyDefinition User PropertyValue
ProfilePropertyDefinitionID and UserID are both foreign keys, so after creating the ProfileProperty object ProfileProperty I select User and ProfilePropertyDefinition from my tables to populate ProfileProperty with the corresponding objects.
Then, when I try to execute AddObject by passing an object with these variables, I get an error:
InnerException = {"Cannot insert a NULL value in the" PropertyName "column, table 'mydbserver.dbo.ProfilePropertyDefinitions', the column does not allow NULL values. INSERT does not work. \ R \ nApplication is complete." }
I took a break to check that the object I passed in was holding, and it has the following:
ProfilePropertyID = -1 ProfilePropertyDefinition = { ProfilePropertyDefinitionID = 3 PropertyName = "First Name" } User = PropertyValue = "Matt"
Questions
- Why is it said that
PropertyName is null when it is there? - Why is he trying to add the
ProfilePropertyDefinition object to the ProfilePropertyDefinition table in the first place? (I don't want it to add or update related objects)
Service Level AddProfile()
public int AddProfile(ProfilePropertyViewModel property) { int objId = -1; ProfileProperty profile = null; if (ValidateProfile(property)) { try { using (DbTransaction transaction = _profileRepository.BeginTransaction()) { profile = ProfilePropertyTranslator.ViewToDomain(property); profile.User = _userRepository.SelectByKey(UserColumns.UserName, property.UserName); profile.ProfilePropertyDefinitionReference.EntityKey = new EntityKey("GraffytysDBEntities.ProfilePropertyDefinition", "ProfilePropertyDefinitionID", property.ProfilePropertyDefinitionID); _profileRepository.Add(profile); if (_profileRepository.Save() >= 0) { transaction.Commit(); objId = property.ProfilePropertyId; } } } catch(Exception ex) { throw ex; } } return objId; }
Repository Add() :
public void Add(E entity) { _ctx.AddObject(entity.GetType().Name, entity); }