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

本文关键字:2个 实体 属性 导航 MVC oData 绑定 net | 更新日期: 2023-09-27 18:13:16

我是。net MVC新手,第一次尝试创建oData web服务。我有使用实体框架引用的3个表:组,子组,链接。

我使用了开箱即用的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生成的更改,它们看起来像这样:

组:

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; }
}
}

如果需要,我可以发布链接模型,但我认为这是与组/子组相同的问题。

任何帮助都将非常感激!

编辑:这是我的数据集:

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);
    }
}
}

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

这是在我的WebAPIConfig中引用错误实体集的错误。我将其更新为以下内容以解决问题:

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