.NET MVC oData将导航属性绑定到2个实体集

时间:2015-09-18 15:13:04

标签: c# asp.net-mvc entity-framework odata

我是.NET MVC的新手,并且是第一次尝试创建oData Web服务。我有3个使用Entity Framework引用的表:Groups,SubGroups,Links。

我使用了开箱即用的Visual Studio oData EF创建3个表,并且Groups web服务似乎工作正常。但是,SubGroups表显示以下错误:

Cannot automatically bind the navigation property 'SubGroups' on entity type 'Quick_Links.Models.Group' for the source entity set 'Groups' because there are two or more matching target entity sets. 

我的模型没有从VS生成的模型中更改,它们如下所示:

GROUP:

namespace Quick_Links.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

public partial class Group
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Group()
    {
        SubGroups = new HashSet<SubGroup>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int GroupID { get; set; }

    public bool? GroupActive { get; set; }

    [StringLength(50)]
    public string GroupName { get; set; }

    [StringLength(50)]
    public string GroupSecurity { get; set; }

    [StringLength(50)]
    public string GroupAdmin { get; set; }

    [StringLength(50)]
    public string UpdateByEID { get; set; }

    public DateTime? UpdateDatetime { get; set; }

    [StringLength(50)]
    public string CreateByEID { get; set; }

    public DateTime? CreateDatetime { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SubGroup> SubGroups { get; set; }
}
}

亚群:

namespace Quick_Links.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

public partial class SubGroup
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public SubGroup()
    {
        Links = new HashSet<Link>();
    }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SubGroupID { get; set; }

    public bool? SubGroupActive { get; set; }

    public int? GroupID { get; set; }

    [StringLength(50)]
    public string SubGroupName { get; set; }

    public int? SubGroupOrder { get; set; }

    [StringLength(50)]
    public string SubGroupSecurity { get; set; }

    [StringLength(50)]
    public string UpdateByEID { get; set; }

    public DateTime? UpdateDatetime { get; set; }

    [StringLength(50)]
    public string CreateByEID { get; set; }

    public DateTime? CreateDatetime { get; set; }

    public virtual Group Group { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Link> Links { get; set; }
}
}

如果需要,我可以发布Links模型,但我认为它与Groups / SubGroups的问题相同。

非常感谢任何帮助!

编辑:这是我的数据集:

namespace Quick_Links.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

public partial class QuickLinks : DbContext
{
    public QuickLinks()
        : base("name=QuickLinks")
    {
    }

    public virtual DbSet<Group> Groups { get; set; }
    public virtual DbSet<Link> Links { get; set; }
    public virtual DbSet<SubGroup> SubGroups { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Group>()
            .Property(e => e.GroupName)
            .IsUnicode(false);

        modelBuilder.Entity<Group>()
            .Property(e => e.GroupSecurity)
            .IsUnicode(false);

        modelBuilder.Entity<Group>()
            .Property(e => e.GroupAdmin)
            .IsUnicode(false);

        modelBuilder.Entity<Group>()
            .Property(e => e.UpdateByEID)
            .IsUnicode(false);

        modelBuilder.Entity<Group>()
            .Property(e => e.CreateByEID)
            .IsUnicode(false);

        modelBuilder.Entity<Link>()
            .Property(e => e.LinkName)
            .IsUnicode(false);

        modelBuilder.Entity<Link>()
            .Property(e => e.LinkURL)
            .IsUnicode(false);

        modelBuilder.Entity<Link>()
            .Property(e => e.LinkSecurity)
            .IsUnicode(false);

        modelBuilder.Entity<Link>()
            .Property(e => e.UpdateByEID)
            .IsUnicode(false);

        modelBuilder.Entity<Link>()
            .Property(e => e.CreateByEID)
            .IsUnicode(false);

        modelBuilder.Entity<SubGroup>()
            .Property(e => e.SubGroupName)
            .IsUnicode(false);

        modelBuilder.Entity<SubGroup>()
            .Property(e => e.SubGroupSecurity)
            .IsUnicode(false);

        modelBuilder.Entity<SubGroup>()
            .Property(e => e.UpdateByEID)
            .IsUnicode(false);

        modelBuilder.Entity<SubGroup>()
            .Property(e => e.CreateByEID)
            .IsUnicode(false);
    }
}
}

1 个答案:

答案 0 :(得分:0)

引用我的WebAPIConfig中设置的错误实体时出错。我将其更新为以下内容以解决问题:

builder.EntitySet<Group>("Groups");
        builder.EntitySet<SubGroup>("SubGroups");
        builder.EntitySet<Link>("Links");