0
저장시 최종 속성을 자동으로 설정하고 각 연결에서 SETCONTEXT를 설정해야하기 때문에 SaveChanges 메서드를 재정의했습니다.DbEntityType에서 원래 형식 가져 오기
public override int SaveChanges()
{
// Use basic SaveChanges if SessionInfo is not initialized
if (SessionInfo.ContextInfo == null)
{
return base.SaveChanges();
}
// SessionInfo was initialized, so use custom logic now
// Set the SqlId according to sessioninfo for each entity to add
foreach (DbEntityEntry entry in ChangeTracker.Entries()
.Where(x => x.State == EntityState.Added))
{
string sqlIdPropertyName =
entry.CurrentValues.PropertyNames.First(x=>x.EndsWith("SqlId");
entry.Property(sqlIdPropertyName).CurrentValue = SessionInfo.ServerSqlId;
}
// Set the IsDeleted boolean to true for each entity to delete
foreach (DbEntityEntry entry in ChangeTracker.Entries()
.Where(x => x.State == EntityState.Deleted))
{
entry.Property("IsDeleted").CurrentValue = true;
entry.State = EntityState.Modified;
}
// Begin custom transaction if SessionInfo was set
this.Database.Connection.Open();
SessionInfo.SetContextInfo(this);
int result = base.SaveChanges();
this.Database.Connection.Close();
return result;
}
당신이 볼 수 있듯이 우리가 데이터베이스에 새 레코드를 추가 할 때의 논리를 저장이 SessionInfo에 따라 개체에 대한 SQLID을 설정 : 다음과 같이 우리의 현재 재정 보인다. 그러나 이제는 PropertyNames.First()에 따라 달라 지므로 위험합니다.
우리가 설정하려는 SqlId의 PropertyName은 항상 POCO 클래스 유형 + SqlId의 이름과 동일하므로 "Invoice"클래스의 경우 "InvoiceSqlId"가됩니다.
어떻게 DbEntityEntry에서 원래 POCO 클래스의 typename을 가져올 수 있습니까?
이하지만 회로 동적 프록시의 형식 이름을 반환, 그리고 원래 클래스 형식을 사용하고있을 수 있습니다 때. – user1374843