這是一個業務邏輯我需要實現:是否可以在ObjectStateManagerChanged處理程序中修改實體對象?
有數據模型,例如多個實體類型:用戶,用戶配置,UserProfileValue,UserExtendedData等
當創建一些實體,如用戶,我需要汽車創建其他實體邏輯鏈接(通常在db中爲1:1關係),例如UserProfile。
每個實體都有一個OnCreating/OnCreated,OnUpdating/OnUpdated方法的控制器,這些方法從我的datacontext類的重寫的SaveChanges方法中調用。
,如果我在User.OnCreating()
方法來實現自動創建那麼最終沒有OnCreating()控制器將因爲收藏addedEntities
不與新條目更新調用所有自動創建的子實體。
我的一個想法是通過處理數據上下文
private void ObjectStateManagerObjectStateManagerChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e)
{
if (e.Action == CollectionChangeAction.Add)
{
var state = ObjectStateManager.GetObjectStateEntry(e.Element).State;
if (state == EntityState.Added)
{
//should call on creating handlers
if (e.Element is User)
{
User user = (User)e.Element;
user.UserProfile = UserProfile.CreateDefaultProfile();
}
}
}
}
但不幸的是我得到NullReferrence異常,如果我做這樣的ObjectStateManagerChanged
執行自動創建相關的實體。即使我嘗試修改ObjectStateManagerObjectStateManagerChanged
內的用戶屬性,我也會得到例外。
有關如何實現所需功能的任何想法或建議?
編輯:
空引用我越來越:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=System.Data.Entity
StackTrace:
at System.Data.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at Edm.Entity.Entities.AddObject(Object entity)
而且Edm.Entity.Entities.AddObject(Object entity)
就是新User
對象添加到上下文。
EDIT2: 不知道我已經改變了,但與上述相同的代碼開始拋出以下異常:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code></code>
<message xml:lang="en-US">An error occurred while processing this request.</message>
<innererror>
<message>Object reference not set to an instance of an object.</message>
<type>System.NullReferenceException</type>
<stacktrace> at System.Data.Objects.DataClasses.EntityReference`1.Exclude()
at System.Data.Objects.DataClasses.RelationshipManager.RemoveRelatedEntitiesFromObjectStateManager(IEntityWrapper wrappedEntity)
at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at System.Data.Services.Providers.ObjectContextServiceProvider.CreateResource(String containerName, String fullTypeName)
at System.Data.Services.UpdatableWrapper.CreateResource(String containerName, String fullTypeName)
at System.Data.Services.Serializers.SyndicationDeserializer.CreateObject(SegmentInfo segmentInfo, Boolean topLevel, SyndicationItem item)
at System.Data.Services.Serializers.SyndicationDeserializer.CreateSingleObject(SegmentInfo segmentInfo)
at System.Data.Services.Serializers.Deserializer.ReadEntity(RequestDescription requestDescription)
at System.Data.Services.Serializers.Deserializer.HandlePostRequest(RequestDescription requestDescription)
at System.Data.Services.DataService`1.HandlePostOperation(RequestDescription description, IDataService dataService)
at System.Data.Services.DataService`1.ProcessIncomingRequest(RequestDescription description, IDataService dataService)
at System.Data.Services.DataService`1.BatchDataService.HandleBatchContent(Stream responseStream)</stacktrace>
</innererror>
</error>
你從哪裏得到'NullReferrence異常'? – Eranga
請參閱我上面的編輯。 – Paul