英孚核心。更新实体而不更新导航属性
EF Core. Update Entity without update navigation properties
我正在尝试像这样更新 Entity EF Core 记录:
public class Customer
{
public int Id { get; set; }
public int Name { get; set; }
...
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public int ZoneId { get; set; }
public virtual Zone Zone { get; set; }
public ICollection<Service> Services { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
}
当我调用 Context.Update() 或 Context.Add() 时,还会更新区域和国家/地区实体。我不希望虚拟属性更新。我正在尝试通过反射获取指示 Entry(virtualProperty).State = EntityState.Detached 的虚拟属性,但我不能。这是我正在尝试的代码。
Type typeOfTEntity = typeof(TEntity);
foreach (var property in typeOfTEntity.GetProperties())
{
if (property.GetGetMethod().IsVirtual)
{
foreach (var member in context.Context.Entry(CurrentItem).Members)
{
if (member.Metadata.Name == property.Name)
{
context.Context.Entry(member).State = EntityState.Detached;
}
}
}
}
我收到错误:"The entity type 'ReferenceEntry' was not found. Ensure that the entity type has been added to the model."
我使用 TEntity 是因为我在通用 Class 中使用了更多实体,并使用相同的方法来更新或添加。
提前致谢。
编辑:
如果我像非泛型类型一样使用实体 (CurrentItem):
(CurrentItem 现在是 Customer,而不是 TEntity)
context.Context.Entry(CurrentItem.Country).State = EntityState.Detached;
context.Context.SaveChanges();
现在运行良好。但我需要使用 TEntity。
如果您不想更新 "subentities",您不应该在获得不想更新的客户时包含它们。
我设法解决了这个问题。不是在条目中插入 属性,而是必须插入 属性 的值。
using (var context = new OpenContext<TContext>(connectionString))
{
var repository = context.UnitOfWork.GetRepository<TEntity, TKey>();
repository.Update(CurrentItem);
// Get the type of TEntity
Type typeOfTEntity = typeof(TEntity);
foreach (var property in typeOfTEntity.GetProperties())
{
// Check the properties that are virtual and not are HashSet
if (property.GetGetMethod().IsVirtual && property.PropertyType.GenericTypeArguments.Count() == 0)
{
object value = property.GetValue(CurrentItem);
// Check that value is not null and value type is an Entity
if (value != null && value.GetType().IsSubclassOf(typeof(Entity<int>)))
{
// Set the value that I don't want to change
context.Context.Entry(value).State = EntityState.Detached;
}
}
}
context.UnitOfWork.SaveChanges();
}
感谢您的帮助。
我正在尝试像这样更新 Entity EF Core 记录:
public class Customer
{
public int Id { get; set; }
public int Name { get; set; }
...
public int CountryId { get; set; }
public virtual Country Country { get; set; }
public int ZoneId { get; set; }
public virtual Zone Zone { get; set; }
public ICollection<Service> Services { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
}
当我调用 Context.Update() 或 Context.Add() 时,还会更新区域和国家/地区实体。我不希望虚拟属性更新。我正在尝试通过反射获取指示 Entry(virtualProperty).State = EntityState.Detached 的虚拟属性,但我不能。这是我正在尝试的代码。
Type typeOfTEntity = typeof(TEntity);
foreach (var property in typeOfTEntity.GetProperties())
{
if (property.GetGetMethod().IsVirtual)
{
foreach (var member in context.Context.Entry(CurrentItem).Members)
{
if (member.Metadata.Name == property.Name)
{
context.Context.Entry(member).State = EntityState.Detached;
}
}
}
}
我收到错误:"The entity type 'ReferenceEntry' was not found. Ensure that the entity type has been added to the model." 我使用 TEntity 是因为我在通用 Class 中使用了更多实体,并使用相同的方法来更新或添加。 提前致谢。
编辑: 如果我像非泛型类型一样使用实体 (CurrentItem): (CurrentItem 现在是 Customer,而不是 TEntity)
context.Context.Entry(CurrentItem.Country).State = EntityState.Detached;
context.Context.SaveChanges();
现在运行良好。但我需要使用 TEntity。
如果您不想更新 "subentities",您不应该在获得不想更新的客户时包含它们。
我设法解决了这个问题。不是在条目中插入 属性,而是必须插入 属性 的值。
using (var context = new OpenContext<TContext>(connectionString))
{
var repository = context.UnitOfWork.GetRepository<TEntity, TKey>();
repository.Update(CurrentItem);
// Get the type of TEntity
Type typeOfTEntity = typeof(TEntity);
foreach (var property in typeOfTEntity.GetProperties())
{
// Check the properties that are virtual and not are HashSet
if (property.GetGetMethod().IsVirtual && property.PropertyType.GenericTypeArguments.Count() == 0)
{
object value = property.GetValue(CurrentItem);
// Check that value is not null and value type is an Entity
if (value != null && value.GetType().IsSubclassOf(typeof(Entity<int>)))
{
// Set the value that I don't want to change
context.Context.Entry(value).State = EntityState.Detached;
}
}
}
context.UnitOfWork.SaveChanges();
}
感谢您的帮助。