如何解决如何在调用 AddAsync 后加载相关实体而不对数据库进行另一次往返
调用AddAsync
后如何加载相关实体?
我有一个看起来像这样的存储库方法
public virtual async Task<TEntity> AddAsync(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
try
{
entity.CreatedOn = entity.UpdatedOn = DateTime.Now;
var newEntity = await Entities.AddAsync(entity);
var newEntityToRet = newEntity.Entity;
_context.SaveChanges();
return newEntityToRet;
}
catch (DbUpdateException exception)
{
//ensure that the detailed error text is saved in the Log
throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception),exception);
}
}
例如尝试插入一个看起来像这样的订单,并且只传递 StatusId
和 TradingActionId
才能使 Add 安全
public class Order
{
public int Id { get; set; }
public bool IsDeleted { get; set; }
public string CreatedBy { get; set; }
public string UpdatedBy { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public string Symbol { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public int StatusId { get; set; }
public OrderStatus Status { get; set; }
public int TradingActionId { get; set; }
public TradingAction TradingAction { get; set; }
public string Notes { get; set; }
}
var order = new Order
{
TradingActionId = 1,StatusId = 1,Notes = source.Notes,Price = source.Price,Symbol = source.Symbol,Quantity = source.Quantity,CreatedOn = dateTimeNow,UpdatedOn = dateTimeNow,UpdatedBy = "test",CreatedBy = "test"
};
问题在于,如果我需要返回具有某些导航属性的新实体。我的以下方法不起作用,但显示了我需要保存实例并同时返回子属性的想法。
public virtual async Task<TEntity> AddAsync(TEntity entity,string[] include = null)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
try
{
entity.CreatedOn = entity.UpdatedOn = DateTime.Now;
var newEntity = await Entities.AddAsync(entity);
var newEntityToRet = newEntity.Entity;
_context.SaveChanges();
if(include != null)
{
foreach (var navProp in include)
{
try
{
var memberEntry = _context.Entry(newEntityToRet).Member(navProp);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
if (memberEntry is DbCollectionEntry collectionMember)
collectionMember.Load();
if (memberEntry is DbReferenceEntry referenceMember)
referenceMember.Load();
}
}
return newEntityToRet;
}
catch (DbUpdateException exception)
{
//ensure that the detailed error text is saved in the Log
throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception),exception);
}
}
我使用的 EF Core 版本是 Microsoft.EntityFrameworkCore.SqlServer 3.1.4
关于如何创建通用 repo 方法并返回所需数据而无需再次往返数据库的任何建议?
解决方法
插入新实体后,您始终可以使用 Load()
或 LoadAsync()
方法显式加载其相关实体。但是,即使它不执行典型的 LINQ
方法/查询(就像您编写的获取一些相关数据的方法),EF 也需要为每次显式加载向数据库提交新查询。因此,它确实不会为您节省任何数据库之旅。
由于您已经尝试在代码中使用 Load()
方法,我假设您的意图只是为了避免编写新的 LINQ
查询(需要获取相关数据),并且不要避免数据库旅行。如果是这种情况,您可以尝试以下操作 -
public async Task<TEntity> AddAsync(TEntity entity,params string[] includes)
{
entity.CreatedOn = entity.UpdatedOn = DateTime.Now;
var newEntry = Entities.Add(entity);
await _context.SaveChangesAsync(); // trip to database
foreach (var navProp in includes)
{
if (newEntry.Navigation(navProp).Metadata.IsCollection())
{
await newEntry.Collection(navProp).LoadAsync(); // trip to database
}
else
{
await newEntry.Reference(navProp).LoadAsync(); // trip to database
}
}
return newEntry.Entity;
}
你可以像 -
var addedOrder = await orderRepository.AddAsync(order,"Status","TradingAction");
注意事项 -
- 我正在使用
params
传递一个或多个参数 - 要添加/插入新实体,请使用
Add
或Attach
方法。除非您正在处理像SequenceHiLo
这样的值生成器,否则您真的不需要使用AddAsync
方法。详情 -AddAsync<TEntity>
类型安全的实现是 -
public async Task<TEntity> AddAsync(TEntity entity,params Expression<Func<TEntity,object>>[] includes)
{
entity.CreatedOn = entity.UpdatedOn = DateTime.Now;
var newEntry = Entities.Add(entity);
await _context.SaveChangesAsync(); // trip to database
foreach (var navProp in includes)
{
string propertyName = navProp.GetPropertyAccess().Name;
if (newEntry.Navigation(propertyName).Metadata.IsCollection())
{
await newEntry.Collection(propertyName).LoadAsync(); // trip to database
}
else
{
await newEntry.Reference(propertyName).LoadAsync(); // trip to database
}
}
return newEntry.Entity;
}
以便您可以传递导航属性,例如 -
var addedOrder = await orderRepository.AddAsync(order,p => p.Status,p => p.TradingAction);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。