Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Entity Framework trouble

I'm trying to add a ProfileProperty into the ProfileProperties table using ObjectContext.AddObject.

The db fields for the ProfileProperties table are:

ProfilePropertyID
ProfilePropertyDefinitionID
UserID
PropertyValue

The db fields for the ProfilePropertyDefinitions table are:

ProfilePropertyDefinitionID
PropertyName

The variables for the ProfileProperty object passed in are:

ProfilePropertyID
ProfilePropertyDefinition
User
PropertyValue

The ProfilePropertyDefinitionID and UserID both are foreign keys, so after creating the ProfileProperty object I select the User and the ProfilePropertyDefinition from their tables to fill the ProfileProperty with the related objects.

Then, when I try to AddObject, passing in an object with those variables I get an error:

InnerException = {"Cannot insert the value NULL into column 'PropertyName', table 'mydbserver.dbo.ProfilePropertyDefinitions'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

I did a break to check to see what the object I passed in was holding and it has this:

ProfilePropertyID = -1
ProfilePropertyDefinition = 
    { ProfilePropertyDefinitionID = 3
      PropertyName = "First Name" }
User = /*Not going  to put details here, but assume the user object is there*/
PropertyValue = "Matt"

Questions

  1. Why is saying that the PropertyName is null when it's there?
  2. Why is it trying to add the ProfilePropertyDefinition object into the ProfilePropertyDefinitions table in the first place? (I don't want it to add or update related objects)

Service Layer 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);
    }
like image 370
Matt Avatar asked Jan 03 '10 18:01

Matt


1 Answers

This will propably solve your problem and is more efficient way to set foreign key properties:

profileProperty.ProfilePropertyDefinitionReference.EntityKey = new EntityKey("MyEntities.ProfilePropertyDefinitions", "ProfilePropertyDefinitionID", 3);

You have to replace 'MyEntities' with your db model name.

You could also check if you are adding ProfilePropertyDefinition to your ObjectContext somewhere else in the code. It could be the problem, not this part of the code.

like image 134
LukLed Avatar answered Oct 23 '22 05:10

LukLed