我想向我的實體添加一些自定義邏輯。每個實體都有一個CreatedBy & LastUpdatedBy屬性。實體框架4自定義邏輯保存更改
我已經添加了下面的代碼工作正常,如果我添加例如一個人和他們的地址在旅途中。
但是,如果我添加Person並調用context.SaveChanges(),然後添加他們的地址並調用context.SaveChanges()。該代碼執行但「entry」的值爲空,因此CreatedBy & LastUpdatedBy未填充。
CustomRepository.CurrentUser
只是獲取用戶名。
ObjectSet = context.CreateObjectSet<TEntity>();
BusinessEntities.Person person = new BusinessEntities.Person()
{
TitleRef = 123,
FirstName = "FirstName",
Surname = "Surname",
PhoneNumber = "PhoneNumber",
MobileNumber = "MobileNumber",
EmailAddress = "EmailAddress",
};
ObjectSet.AddObject(entity);
Context.SaveChanges();
PersonAddress personAddress = new BusinessEntities.PersonAddress
{
StartDate = DateTime.Now,
EndDate = DateTime.Now,
OtherResidents = "OtherResidents",
CurrentAddress = true,
Address = new Address
{
Address1 = "Address1",
Address2 = "Address2",
Address3 = "Address3"
}
};
Context.SaveChanges();
public override int SaveChanges(SaveOptions options)
{
foreach (ObjectStateEntry entry in
ObjectStateManager.GetObjectStateEntries(
EntityState.Added | EntityState.Modified))
{
System.Collections.ObjectModel.ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
.DataRecordInfo.FieldMetadata;
FieldMetadata createdField = fieldsMetaData
.Where(f => f.FieldType.Name == "CreatedBy").FirstOrDefault();
FieldMetadata modifiedField = fieldsMetaData
.Where(f => f.FieldType.Name == "LastUpdatedBy").FirstOrDefault();
if (modifiedField.FieldType != null)
{
string fieldTypeName = modifiedField.FieldType.TypeUsage.EdmType.Name;
if (fieldTypeName == PrimitiveTypeKind.String.ToString())
{
if (entry.CurrentValues[createdField.Ordinal].ToString() == null ||
entry.CurrentValues[createdField.Ordinal].ToString() == String.Empty)
{
entry.CurrentValues.SetString(createdField.Ordinal, CustomRepository.CurrentUser);
}
entry.CurrentValues.SetString(modifiedField.Ordinal, CustomRepository.CurrentUser);
}
}
}
return base.SaveChanges(options);
}
------------------------原始方法----------------- ------------
private void Initialize()
{
// Creating proxies requires the use of the ProxyDataContractResolver and
// may allow lazy loading which can expand the loaded graph during serialization.
ContextOptions.ProxyCreationEnabled = false;
ObjectMaterialized += new ObjectMaterializedEventHandler(HandleObjectMaterialized);
OnContextCreated();
}
private void OnContextCreated()
{
this.SavingChanges += Context_SavingChanges;
}
private void Context_SavingChanges(object sender, EventArgs e)
{
try
{
IEnumerable<ObjectStateEntry> objectStateEntries =
from ose
in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added
| EntityState.Modified)
where ose.Entity != null
select ose;
foreach (ObjectStateEntry entry in objectStateEntries)
{
System.Collections.ObjectModel.ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues
.DataRecordInfo.FieldMetadata;
FieldMetadata createdField = fieldsMetaData
.Where(f => f.FieldType.Name == "CreatedBy").FirstOrDefault();
FieldMetadata modifiedField = fieldsMetaData
.Where(f => f.FieldType.Name == "LastUpdatedBy").FirstOrDefault();
if (modifiedField.FieldType != null)
{
string fieldTypeName = modifiedField.FieldType.TypeUsage.EdmType.Name;
if (fieldTypeName == PrimitiveTypeKind.String.ToString())
{
if (entry.CurrentValues[createdField.Ordinal].ToString() == null ||
entry.CurrentValues[createdField.Ordinal].ToString() == String.Empty)
{
entry.CurrentValues.SetString(createdField.Ordinal, CustomRepository.CurrentUser);
}
entry.CurrentValues.SetString(modifiedField.Ordinal, CustomRepository.CurrentUser);
}
}
}
}
}
您還應該顯示您的「人員」和「地址」實體,以及如何添加它們。 – 2011-04-26 17:51:17