更新EF中的实体属性,其中property是另一个实体

Mig*_*ura 7 .net c# entity-framework

我正在使用Entity Framework 6,我需要更新实体的属性.

我有以下实体:

public class File 
{
  public Int32 Id { get; set; }
  public Byte Data { get; set; }
  public DateTime Updated { get; set; }
  public virtual Mime Mime { get; set; }
}
public class Mime 
{
  public Int32 Id { get; set; }
  public String Name { get; set; }
  public virtual ICollection<File> Files { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我使用了以下内容:

_repository.Update<File>(file, x => x.Data, x => x.Mime, x => x.Updated);
Run Code Online (Sandbox Code Playgroud)

存储库方法如下:

public void Update<T>(T entity,
                      params Expression<Func<T, Object>>[] properties)
                      where T : class 
{

  _context.Set<T>().Attach(entity);

  foreach (var property in properties) 
  {
    MemberExpression expression =
                     property.Body is MemberExpression ? 
                     (MemberExpression)property.Body : 
                     (MemberExpression)(((UnaryExpression)property.Body)
                                                                  .Operand);
    _context.Entry<T>(entity)
            .Property(expression.Member.Name).IsModified = true;
  }
}
Run Code Online (Sandbox Code Playgroud)

这适用于Data和更新属性,但不适用于Mime.我收到错误:

'File'类型的属性'Mime'不是原始属性或复杂属性.Property方法只能与原始或复杂属性一起使用.使用Reference或Collection方法.

是否可以将其工作并将其集成到我的存储库方法中?

Ert*_*maa 0

是的,我认为这是可以做到的。这里的问题是,我没有看到任何简单的方法来检查属性是否是表的一部分,或者是否是导航属性。因此很难称之为正确的行为。

如果您感兴趣,请查看 EF6 源代码,InternalEntityEntry.cs->Property(..)它通过元数据进行大量属性验证。

主要思想是基本上扫描您的概念模型,并确定该属性何时是导航属性(例如,该属性是否通向另一个表),或者它是否是复杂/原始的。

据此,您可以调用正确的功能。

var propertyName = expression.Member.Name;                                              
var propertyType = __get_property_type__(propertyName);

if(propertyType==Property || propertyType==Complex)
{
    _context.Entry<T>(entity)
        .Property(propertyName).IsModified = true;

    continue;
}

if(propertyType==Navigational){

    // hm, do we need Attach it first?!
    // not sure.. have to test first.
    dynamic underlyingReference = entity.GetType()
            .GetProperty(propertyName)
            .GetValue(entity, null);

    _context.Entry(underlyingReference).State = EntityState.Modified;
}
Run Code Online (Sandbox Code Playgroud)

这里的要点是让它发挥__get_property_type__作用。Microsoft.Data.Edm.dll可以 让您使用概念模型,但我认为这并不那么容易。

这是 EF6 检测我们是否正在处理引用属性的方式:

EdmMember member;
EdmEntityType.Members.TryGetValue(propertyName, false, out member);

var asNavProperty = member as NavigationProperty;
// if asNavProperty!=null, we have navigation property.
Run Code Online (Sandbox Code Playgroud)