Вот моя сущность:
Код: Выделить всё
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)
}
Код: Выделить всё
public class Repository : IRepositoryAsync where T : class, IObjectState
{
private readonly IDataContextAsync context;
private readonly DbSet dbSet;
//private read-only IUnitOfWorkAsync uow;
public Repository( IDataContextAsync _context )
{
context = _context;
dbSet = ( (DbContext)context ).Set();
}
public virtual IDataContextAsync Context { get { return context; } }
public IDbSet DbSet { get { return dbSet; } }
public T FindOne( Expression predicate )
{
return dbSet.SingleOrDefault( predicate );
}
public IQueryable FindBy( Expression 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 );
}
}
Код: Выделить всё
public class UnitOfWork : IUnitOfWorkAsync
{
private IDataContextAsync context;
private IOperationStatus opStatus;
private Dictionary 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();
//}
public UnitOfWork( IDataContextAsync _context, IOperationStatus _opStatus )
{
context = _context;
opStatus = _opStatus;
repositories = new Dictionary();
}
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 Repository() where T : class, IObjectState
{
return RepositoryAsync();
}
public async Task 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;
}
}