SaveChanges()를 재정의하지만보다 효과적이고 깔끔한 방법이 있는지 알고 싶습니다. 나는 정말로 switch 문을 좋아하지 않는다. 아래에서는 문제를 설명하고 완전한 해결책을 설명했습니다.SaveChanges를 재정 의하여 비즈니스 규칙을 구현하십시오.
감사
문제 사용자가 삽입 뭔가 업데이트를 수행하거나 여러 테이블에 분산 데이터, 업데이트를 열 NewClub.LastActivityDate = DateTime.Now
의 일부를 삭제 할 때마다모델
public class NewClub
{
//Primary Key
public int Id { get; set; }
[...]
public DateTime LastActivityDate { get; set; }
}
public class NewClubProspect
{
//Primary Key
[Key]
public int Id { get; set; }
//Foreign Key
public int NewClubId { get; set; }
[..]
public virtual NewClub NewClub { get; set; }
}
public class NewClubCounselor
{
[Key]
public int Id { get; set; }
//Foreign Key
public int NewClubId { get; set; }
[...]
public NewClub NewClub { get; set; }
}
// Several more model classes like these ...
솔루션
public class MyContext : DbContext
{
public override int SaveChanges()
{
var entityInfoStr = String.Empty;
var saveSuccess = false;
var newClubPrimaryKeyId = 0;
ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;
List<ObjectStateEntry> objectStateEntryList =
ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
| EntityState.Unchanged
| EntityState.Modified
| EntityState.Deleted).ToList();
foreach (ObjectStateEntry entry in objectStateEntryList)
{
//Skip over relationships
if (entry.IsRelationship) continue;
//Make sure the entity is a member of the schema: NewClub[...]
if (SecurityHelper.IsValidNewClubTableName(entry.EntitySet.Name))
{
switch (entry.EntitySet.Name)
{
case "NewClubs":
var nc = entry.Entity as NewClub;
if (nc != null) { newClubPrimaryKeyId = nc.Id; }
break;
case "NewClubBuilders":
var ncb = entry.Entity as NewClubBuilder;
if (ncb != null) { newClubPrimaryKeyId = ncb.NewClubId; }
break;
case "NewClubCaseAnswers":
var ncca = entry.Entity as NewClubCaseAnswer;
if (ncca != null) { newClubPrimaryKeyId = ncca.NewClubId; }
break;
case "NewClubCommunityLeaders":
var nccl = entry.Entity as NewClubCommunityLeader;
if (nccl != null) { newClubPrimaryKeyId = nccl.NewClubId; }
break;
case "NewClubCounselors":
var ncc = entry.Entity as NewClubCounselor;
if (ncc != null) { newClubPrimaryKeyId = ncc.NewClubId; }
break;
case "NewClubEmails":
var nce = entry.Entity as NewClubEmail;
if (nce != null) { newClubPrimaryKeyId = nce.NewClubId; }
break;
case "NewClubKitOrders":
var ncko = entry.Entity as NewClubKitOrder;
if (ncko != null) { newClubPrimaryKeyId = ncko.NewClubId; }
break;
case "NewClubOrganizationChecklists":
var ncoc = entry.Entity as NewClubKitOrder;
if (ncoc != null) { newClubPrimaryKeyId = ncoc.NewClubId; }
break;
case "NewClubProspects":
var ncp = entry.Entity as NewClubProspect;
if (ncp != null) { newClubPrimaryKeyId = ncp.NewClubId; }
break;
case "NewClubRecruiterTrainingSchedules":
var ncrts = entry.Entity as NewClubRecruiterTrainingSchedule;
if (ncrts != null) { newClubPrimaryKeyId = ncrts.NewClubId; }
break;
case "NewClubRecruitingMember":
var ncrm = entry.Entity as NewClubRecruitingMember;
if (ncrm != null) { newClubPrimaryKeyId = ncrm.NewClubId; }
break;
case "NewClubRecruitingTeamEvent":
var ncrte = entry.Entity as NewClubRecruitingTeamEvent;
if (ncrte != null) { newClubPrimaryKeyId = ncrte.NewClubId; }
break;
case "NewClubSponsor":
var ncs = entry.Entity as NewClubSponsor;
if (ncs != null) { newClubPrimaryKeyId = ncs.NewClubId; }
break;
}
//Update the NewClub.LastActivityDate column
if (newClubPrimaryKeyId > 0)
{
string q = @"UPDATE NewClub SET LastActivityDate='" + DateTime.Now + "' WHERE Id="+newClubPrimaryKeyId;
using (var context = new MyContext())
{
var result = context.Database.ExecuteSqlCommand(q);
}
}
}
}
try
{
saveSuccess = base.SaveChanges() > 0;
}
catch (Exception e)
{
string ex = e.ToString();
//Handle exception
}
return saveSuccess ? 1 : 0;
}
}
여기에서 좋은 대답을 얻을 수는 있지만, 그렇지 않은 경우 코드 검토 stackexchange에서 질문하십시오. –