gpt4 book ai didi

c# - 覆盖 SaveChanges() 的最佳方法

转载 作者:太空狗 更新时间:2023-10-29 18:16:01 25 4
gpt4 key购买 nike

我们在一个项目上工作了 1 个月,有 6 个实体,与其他实体没有任何关系。它们都是简单的实体。

我们为每个实体的操作创建了 6 个不同的类。 SaveOrUpdateEntity() 类的方法和你想的差不多。是这样的:

public static ErrorType SaveOrUpdateEntity(Entity entity, int userID)
{
try
{
using (DataEntities ctx = new DataEntities())
{
if (entity != null)
{
if (entity.entityID == 0)
{
entity.CreateDate = DateTime.Now;
entity.CreatedBy = userID;

ctx.Entry(entity).State = EntityState.Added;
}
else
{
entity.ModifyDate = DateTime.Now;
entity.ModifiedBy = userID;

ctx.Entry(entity).State = EntityState.Modified;
}
}

ctx.SaveChanges();
}

return ErrorType.NoError;
}
catch (Exception ex)
{
return ErrorType.SaveError;
}
}

如果 SaveOrUpdateEntity() 方法通过覆盖 SaveChanges() 方法更短更通用,那将非常有帮助。根据其他关于覆盖 SaveChanges() 方法的问题、文章和帖子,实现存储实体状态的接口(interface)是一个很好的解决方案,但我也想知道其他解决方案。

因为我是新手,所有的答案将不胜感激。

谢谢。

最佳答案

您可以执行以下操作

1- 在您的应用程序中创建一个接口(interface),所有具有以下属性的类都将实现此接口(interface):Id、CreatedDate、CreatedBy、ModifiedDate、ModifiedBy

public interface ITrack
{
int Id{get; set;}
int CreatedBy{get; set;}
DateTime CreatedDate{get; set;}
int? ModifiedBy{get; set;} // int? because at first add, there is no modification
DateTime? ModifiedBy {get; set;}
}

Best practices Define the CreatedBy and ModifiedBy as string which will be good for performance and maintenance

2- 添加一个类 TrackableEntry 实现接口(interface) ITrack

public abstract class TrackableEntry : ITrack
{
public int Id{get; set;}
public int CreatedBy{get; set;}
public DateTime CreatedDate{get; set;}
public int? ModifiedBy{get; set;}
public DateTime? ModifiedBy {get; set;}
}

3- 从所有类中删除接口(interface)中提到的属性,让这些类直接从 TrackableEntry 实现

public class A: TrackableEntry
{
//public int Id{get; set;}
//public int CreatedBy{get; set;}
//public DateTime CreatedDate{get; set;}
//public int? ModifiedBy{get; set;}
//public DateTime? ModifiedBy {get; set;}
}

4- 在您的 DbContext 文件中覆盖您的 SaveChanges 并添加属性 UserIdUserName 如果您遵循*最佳实践*部分

public int UserId{get; set;}

public override int SaveChanges()
{
this.ChangeTracker.DetectChanges();
var added = this.ChangeTracker.Entries()
.Where(t => t.State == EntityState.Added)
.Select(t => t.Entity)
.ToArray();

foreach (var entity in added)
{
if (entity is ITrack)
{
var track = entity as ITrack;
track.CreatedDate = DateTime.Now;
track.CreatedBy = UserId;
}
}

var modified = this.ChangeTracker.Entries()
.Where(t => t.State == EntityState.Modified)
.Select(t => t.Entity)
.ToArray();

foreach (var entity in modified)
{
if (entity is ITrack)
{
var track = entity as ITrack;
track.ModifiedDate = DateTime.Now;
track.ModifiedBy = UserId;
}
}
return base.SaveChanges();
}

最后,当您要调用 SaveChanges 方法时,请确保您设置了 UserIdUserName

var entities=new Entities(); // assuming that your DbContext file called Entities
// code for adding or deletion or modification here
entities.As.Add(new A(){...});

// ....

entities.UserId=MyUser;
entities.SaveChanges();

希望对你有帮助

关于c# - 覆盖 SaveChanges() 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com