ProfileProperty
を使用してProfileProperties
テーブルに を追加しようとしていますObjectContext.AddObject
。
テーブルのdb
フィールドは次のProfileProperties
とおりです。
ProfilePropertyID
ProfilePropertyDefinitionID
UserID
PropertyValue
テーブルのdb
フィールドは次のProfilePropertyDefinitions
とおりです。
ProfilePropertyDefinitionID
PropertyName
渡されるオブジェクトの変数は次のProfileProperty
とおりです。
ProfilePropertyID
ProfilePropertyDefinition
User
PropertyValue
ProfilePropertyDefinitionID
とはUserID
どちらも外部キーであるため、オブジェクトを作成した後、とをテーブルからProfileProperty
選択して、 に関連オブジェクトを入力します。User
ProfilePropertyDefinition
ProfileProperty
次に、AddObject
これらの変数を含むオブジェクトを渡そうとすると、エラーが発生します。
InnerException = {"値 NULL を列 'PropertyName'、テーブル 'mydbserver.dbo.ProfilePropertyDefinitions' に挿入できません。列は NULL を許可しません。INSERT は失敗します。\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"
質問
PropertyName
そこにあるのに null だと言っているのはなぜですか?- そもそも
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);
}