使用Entity Framework遇到问题

6

我正在尝试使用ObjectContext.AddObject将一个ProfileProperty添加到ProfileProperties表中。

ProfileProperties表的db字段为:

ProfilePropertyID
ProfilePropertyDefinitionID
UserID
PropertyValue

ProfilePropertyDefinitions表中的db字段为:

ProfilePropertyDefinitionID
PropertyName
ProfileProperty 对象的变量如下:
ProfilePropertyID
ProfilePropertyDefinition
User
PropertyValue
ProfilePropertyDefinitionIDUserID都是外键,因此在创建ProfileProperty对象后,我从它们的表中选择UserProfilePropertyDefinition来填充相关对象以填充ProfileProperty
然后,当我尝试使用这些变量传递一个对象进行AddObject时,会出现错误:

InnerException = {"无法将值NULL插入到列“PropertyName”中,表“mydbserver.dbo.ProfilePropertyDefinitions”不允许空值。插入失败。\r\n已终止该语句。"}

我打了一个断点来检查传入的对象持有什么,结果如下:
ProfilePropertyID = -1
ProfilePropertyDefinition = 
    { ProfilePropertyDefinitionID = 3
      PropertyName = "First Name" }
User = /*Not going  to put details here, but assume the user object is there*/
PropertyValue = "Matt"

问题

  1. PropertyName存在时,为什么会说它为null?
  2. 为什么它要将ProfilePropertyDefinition对象添加到ProfilePropertyDefinitions表中?(我不想添加或更新相关对象)

服务层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;
 }

代码库 Add() 方法:

    public void Add(E entity)
    {
        _ctx.AddObject(entity.GetType().Name, entity);
    }

1
所以你正在使用现有的“User”和“ProfilePropertyDefinition”创建一个新的“ProfileProperty”。你是否从调用“AddObject”的相同上下文中选择现有对象? - Sander Rijken
你可以尝试修改代码并设置ProfilePropertyDefinitionReference.EntityKeyUserReference.EntityKey。你不需要先从数据库中选择它,这可能会解决你的问题。 - LukLed
是的,它们都来自同一个上下文。 - Matt
我在翻译器后面设置了一个断点,以检查对象是否被正确创建。它所做的就是获取值并从中创建一个对象,与我使用 new ProfileProperty(/我的值在这里/) 完全相同。 - Matt
只需编写最简单的函数来添加ProfileProperty。不要使用ProfilePropertyViewModel,也不要使用ProfilePropertyTranslator。它能正常工作吗? - LukLed
显示剩余5条评论
1个回答

6
这可能会解决你的问题,并且是设置外键属性更有效的方法:
profileProperty.ProfilePropertyDefinitionReference.EntityKey = new EntityKey("MyEntities.ProfilePropertyDefinitions", "ProfilePropertyDefinitionID", 3);

你需要将'MyEntities'替换为你的数据库模型名称。

你也可以检查一下代码中是否在其他地方向你的ObjectContext中添加了ProfilePropertyDefinition,这可能是问题所在,而不是代码的这一部分。


你确定没有使用相同的 ObjectContext 添加其他内容吗?你可能正在向同一个 ObjectContext 添加另一个 ProfilePropertyDefinition,从而导致错误。在调用 SaveChanges 之前,请检查它。检查 ProfilePropertyDefinitions 是否包含新行。 - LukLed
不应该是这样的...我刚刚检查了一下,没有新行。这是我调用添加到存储库的位置的代码:_profileRepository.Add(property); 如果(_profileRepository.Save() >= 0) { transaction.Commit(); } - Matt
好的。你能贴上整个代码吗?你也可以查看 SQL Profiler 并查看实际调用了什么 SQL。请记住,您不必调用 _repository.Add(...) 将对象插入 ObjectContext。如果新对象连接到通过 _repository.Add(...) 添加到 ObjectContext 中的另一个对象,则它也将被添加。 - LukLed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接