我使用EF6代碼優先(來自現有數據庫)來執行CRUD操作。我將在這裏寫一些模擬代碼來解釋,因爲由於一些版權問題,我無法粘貼真正的代碼。 這裏是我的實體:EF6 - 有條件地更新SaveChanges()上的實體屬性
public partial class Person : EntityBase
{
public long Id { get; set; } // ID (Primary key)
public string LastName { get; set; } // LastName (length: 50)
public string FirstName { get; set; } // FirstName (length: 50)
public string SocialSecurity { get; set; } // SocialSecurity (length: 50)
public long CreatedById { get; set; } // CreatedByID
public System.DateTime CreatedDate { get; set; } // CreatedDate
public long UpdatedById { get; set; } // UpdatedByID
public System.DateTime UpdatedDate { get; set; } // UpdatedDate
public byte[] TimeStamped { get; set; } // TimeStamped (length: 8)
}
我有一個通用的倉庫做CRUD操作。
public class Repository<T> : IRepositoryAsync<T> where T : class, IObjectState
{
private readonly IDataContextAsync context;
private readonly DbSet<T> dbSet;
//private read-only IUnitOfWorkAsync uow;
public Repository(IDataContextAsync _context)
{
context = _context;
dbSet = ((DbContext)context).Set<T>();
}
public virtual IDataContextAsync Context { get { return context; } }
public IDbSet<T> DbSet { get { return dbSet; } }
public T FindOne(Expression<Func<T, bool>> predicate)
{
return dbSet.SingleOrDefault(predicate);
}
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
return dbSet.Where(predicate);
}
public void Add(T entity)
{
//entity.ObjectStateEnum = ObjectStateEnum.Added;
dbSet.Add(entity);
}
public void Delete(dynamic id)
{
var entity = dbSet.Find(id);
Delete(entity);
}
public void Update(T entity)
{
//entity.ObjectStateEnum = ObjectStateEnum.Modified;
dbSet.Add(entity);
}
}
這裏是我的UnitOfWork實現。代碼片段再一次只是片段。不完整的代碼。
public class UnitOfWork : IUnitOfWorkAsync
{
private IDataContextAsync context;
private IOperationStatus opStatus;
private Dictionary<string, dynamic> repositories;
private ObjectContext objectContext;
private DbTransaction transaction;
private bool disposed;
public IDataContextAsync DataContext { get { return context; } }
//public UnitOfWork()
//{
// context = new RedStoneDbContext(); //???? _context;
// opStatus = new OperationStatus(); //???? _opStatus;
// repositories = new Dictionary<string, dynamic>();
//}
public UnitOfWork(IDataContextAsync _context, IOperationStatus _opStatus)
{
context = _context;
opStatus = _opStatus;
repositories = new Dictionary<string, dynamic>();
}
public IOperationStatus SaveChanges()
{
opStatus.Success = false;
try
{
int numRec = context.SaveChanges();
opStatus.Success = true;
opStatus.RecordsAffected = numRec;
}
catch (SystemException ex)
{
opStatus = opStatus.CreateFromException(ex);
}
return opStatus;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
try
{
if (objectContext != null && objectContext.Connection.State == ConnectionState.Open)
objectContext.Connection.Close();
}
catch (ObjectDisposedException)
{
// do nothing
}
if (context != null)
{
context.Dispose();
context = null;
}
}
disposed = true;
}
public IRepository<T> Repository<T>() where T : class, IObjectState
{
return RepositoryAsync<T>();
}
public async Task<IOperationStatus> SaveChangesAsync()
{
opStatus.Success = false;
try
{
int numRec = await context.SaveChangesAsync();
opStatus.Success = true;
opStatus.Message = "Record successfully saved!";
opStatus.RecordsAffected = numRec;
}
catch (DbUpdateConcurrencyException ex)
{
opStatus = opStatus.CreateFromException(ex);
}
catch (SystemException ex)
{
opStatus = opStatus.CreateFromException(ex);
}
return opStatus;
}
}
所以,這是我的問題。當savechanges被調用時,我想查看實體是否包含一列「SocialSecurity」,如果存在,我查看用戶的角色和權限。如果一切正常,想要讓傳入的SSN保留在數據庫中。如果沒有,我只想讓SaveChanges忽略SocialSecurity屬性,但像往常一樣更新其他所有內容。我找不出一個簡單而有效的方法來做到這一點。任何幫助,高度讚賞。
巴布。
這當然是可能的。但是,'SaveChanges()'絕對不是這種邏輯的地方 – haim770