繁体   English   中英

从基础实体访问子实体的属性

[英]Access child entities' properties from base entity

一年多以后,从我关于SO的第一个问题( 从子实体的属性中过滤基本实体 )开始,我遇到了类似的问题。

我有一个抽象的基类型

public abstract class Base{
}

我有一些继承自此基本类型的子实体

public class Child1 : Base{
    public virtual NavigationProperty NavigationProperty {get; set; }
    public int NavigationPropertyId {get; set}
}

public class Child2 : Base{
    public virtual NavigationProperty NavigationProperty {get; set; }
}

子实体都具有NavigationProperty属性。 而NavigationProperty类就像

 public class NavigationProperty{
    public virtual ICollection<Child1> Child1s {get; set;}
    public virtual Child2 Child2s {get; set;}
 }

Child2和NavigationProperty之间有一对一的映射; Child1和NavigationProperty之间的一对多关系。 为了使这些映射工作,我正在使用TPT。 我的第一个问题是,我可以搬家吗?

  public NavigationProperty NavigationProperty {get; set; }

到基础班?

我正在尝试这一整天并没有任何成功。 如果不可能,我至少可以从基本类型访问NavigationProperty。 在所有的孩子都拥有这个属性后,我尝试了类似的东西

 public abstract class Base{
      public abstract NavigationProperty NavigationProperty {get; set; }
 }
 ....
 public abstract class Child2{
      public override NavigationProperty NavigationProperty {get; set; }
 }

但是实体框架会出现以下错误。

 Sequence contains more than one matching element 

我可以用类似的东西

   public abstract class Base{
      public abstract NavigationProperty GetNavigationProperty();
   }

  public abstract class Child2{
      public override NavigationProperty NavigationProperty {get; set; }
      public override NavigationProperty GetNavigationProperty(){
           return NavigationProperty;
      }
 }

但我不想介绍这些额外的方法。 他们是否有办法更优雅地实现这一目标?

编辑:

我忘了提到我已经尝试了[NotMapped]属性。 我猜EF [NotMapped]属性也是继承的,所以子属性也没有被映射。

我不希望Linq-to-Entites工作。 我不希望能够使用导航属性查询基础实体。 我只是想摆脱GetNavigationProperty和SetNavigationProperty方法。 因此,当我尝试从基类访问NavigationProperty时,它应该被加载到内存中,就是这样。 然而,经过一周的努力,我认为这是不可能的。

我可以搬家吗?

public NavigationProperty NavigationProperty {get; set; }

到基础班?

没有,因为在实体逆特性NavigationPropertyChild1Child2 ,而不是Base 导航属性始终必须是声明类型的属性,并且不能移动到继承链中的基类型。

对于第二个问题,您可以尝试从映射中排除抽象导航属性:

public abstract class Base {
    [NotMapped]
    public abstract NavigationProperty NavigationProperty {get; set; }
}

(或modelBuilder.Entity<Base>().Ignore(b => b.NavigationProperty);使用Fluent API)。

您将无法在任何查询中使用Base.NavigationProperty ,因为您不能将未映射的属性与LINQ-to-Entities一起使用。

编辑x1更新了代码以避免属性名称的文字字符串

一点反思似乎可以完成这项工作。 班级设置;

public class NavigationProperty
{
    public NavigationProperty(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
}

public abstract class Base
{
    public NavigationProperty NavigationProperty
    {
        get
        {
            string propertyName = MethodBase.GetCurrentMethod().Name.Replace("get_", string.Empty);
            PropertyInfo property = this.GetType().GetProperty(propertyName);
            if (property != null)
            {
                nav = (NavigationProperty)property.GetValue(this, new object[] { });
            }

            return nav;
        }
        set
        {
            string propertyName = MethodBase.GetCurrentMethod().Name.Replace("set_", string.Empty);
            PropertyInfo property = this.GetType().GetProperty(propertyName);
            if (property != null)
            {
                property.SetValue(this, value, new object[] { });
            }
        }
    }
}

public class Child1 : Base {
    public NavigationProperty NavigationProperty { get; set; }
    public int NavigationPropertyId { get; set; }
}

public class Child2 : Base{
    public NavigationProperty NavigationProperty { get; set; }
}

在你的代码中;

Child1 c1 = new Child1() { NavigationProperty = new NavigationProperty("child1Value") };
Child2 c2 = new Child2() { NavigationProperty = new NavigationProperty("child2Value") };
Base somebase = c1;
NavigationProperty childNav = somebase.NavigationProperty;
// childNav.Name now contains "child1Value"

这符合您的要求吗? 它比使用abstract方法更笨重但它至少意味着你不必重构每个子类

如果没有为您的方案在Entity Framework中找到支持,您可以尝试这样做:

public interface IHasNavigationProperty {
    NavigationProperty NavigationProperty { get; }
}

public class Child1 : Base, IHasNavigationProperty {
    public NavigationProperty NavigationProperty { get; set; }
}

public class Base {
    public void AMethodThatDoesStuff() {
        if (this is IHasNavigationProperty) {
            var navigationProperty = ((IHasNavigationProperty)this).NavigationProperty;

            /* do stuff with NavigationProperty */
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM