【问题标题】:The specified type member 'Locale_Section' is not supported in LINQ to Entities.LINQ to Entities 不支持指定的类型成员“Locale_Section”。
【发布时间】:2013-03-01 03:12:02
【问题描述】:

我在stackoverflow上遇到了一些与我的here类似的问题,但它们看起来并没有深入到连接表中,而且我看不到任何适用于我的代码的东西,所以我希望有人可以查看我的代码位并告诉我为什么我得到了

LINQ to Entities 不支持指定的类型成员“Locale_Section”。仅支持初始化器、实体成员和实体导航属性。

        private object SetNavItems(string Category, byte CultureID)
    {
        using (var db = new Compleate())
        {
            return (from n in db.Navigation
                    where n.Category == Category && n.Section.Locale_Section.CultureID == CultureID
                    orderby n.Position
                    select new
                    {
                        n.Section.Locale_Section.Title,
                        n.Section.LinkAddress
                    }).ToList();
        }
    }

导航.cs

    [Table("Navigation")]
public class Navigation
{
    [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int NavigationID { get; set; }

    [Required, MaxLength(16), Column(TypeName = "varchar")]
    public string Category { get; set; }

    [Required]
    public Int16 SectionID { get; set; }

    [ForeignKey("SectionID")]
    public virtual Section Section { get; set; }

    [Required]
    public byte Position { get; set; }
}

Ssection.cs

    [Table("Section")]
public class Section
{
    [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int16 SectionID { get; set; }

    public Int64 LogoFileID { get; set; }

    [ForeignKey("LogoFileID")]
    public virtual File File { get; set; }

    [Required, MaxLength(15), Column(TypeName = "varchar")]
    public string RouteName { get; set; }

    [Required, MaxLength(15), Column(TypeName = "varchar")]
    public string Type { get; set; }

    [NotMapped]
    public string LinkAddress
    {
        get
        {
            return Type + "/" + RouteName;
        }
    }

    [NotMapped]
    public virtual Locale_Section Locale_Section { get; set; }
}

Locale_Section.cs

    [Table("Locale_Section")]
public class Locale_Section
{
    [Key, Required, Column(Order = 0)]
    public Int16 SectionID { get; set; }

    [ForeignKey("SectionID")]
    public virtual Section Section { get; set; }

    [Key, Required, Column(Order = 1)]
    public byte CultureID { get; set; }

    [ForeignKey("CultureID")]
    public virtual Culture Culture { get; set; }

    [Required, MaxLength(250)]
    public string Title { get; set; }

    public string Synopsis { get; set; }
}

【问题讨论】:

    标签: c# webforms entity-framework-5 asp.net-4.5


    【解决方案1】:

    您已将Section 实体中的Section_Locale 属性标记为NotMapped。这意味着该属性对于您的映射是未知的,并且无法在 Linq-to-Entities 查询中转换为 SQL。如果不映射它或将所有 Navigation 实例加载到您的应用程序,您就不能使用该属性,例如首先调用 ToListAsEnumerable(这很可能是非常糟糕的解决方案)。

    【讨论】:

    • 如果我删除了NotMapped 属性,那么将在数据库中创建一个列并且需要一个值。我可以做些什么来防止这种情况发生吗? Section Table 不需要 Local_Section 的列,因为 Local_Section 已经有一个映射回 Section(在 SectionID 上)。 Section -> Local_Section 的关系是 One -> Many。
    • 如果不映射,则不能在 Linq-to-entities 查询中使用。如果关系是一对多的,Section 类应该使用包含 Local_section 的集合。不仅是单一参考。
    • 您能否详细说明“包含 Local_Section 的集合”。我对 EFand LINQ 非常陌生。
    • 今天休息时环顾四周,我发现stackoverflow.com/questions/4735981/… 我认为这是我需要建立一对多关系的。我今晚回家时会试试看。如果我在错误的轨道上,请纠正我。
    • 当我将其更改为公共虚拟列表 Locale_Section { get;放; } 那么我就不能再访问 Locale_Section 表中的任何内容了。请帮助我做错了什么?
    猜你喜欢
    • 2015-01-27
    • 2012-07-17
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2018-02-19
    • 2015-07-25
    相关资源
    最近更新 更多