0
如果我有一个用户实体框架,只有一端连接
public class User : BaseEntity
{
public int Id { get; set; }
public AddressId {get;set;}
public Address Address {get; set;}
}
和地址类
public class Address : BaseEntity
{
public int Id { get; set; }
}
因此,只有这是一对一的,只有具有与该连接的用户另一端。
我该如何使用流畅的API来设置它,也可以使我的通用SaveOrUpdate方法考虑到一次性保存图吗?
public static async Task<T> SaveOrUpdateAsync<T>(this GSCMContext context, T updated) where T : BaseEntity
{
if (updated == null)
return null;
if (updated.IsPersisted)
{
T existing = context.Set<T>().Find(updated.Id);
if (existing != null)
{
context.Entry(existing).CurrentValues.SetValues(updated);
await context.SaveChangesAsync();
}
return existing;
}
context.Set<T>().Add(updated);
await context.SaveChangesAsync();
return updated;
}