簡體   English   中英

OData V4的Web Api無法與HasKey一起使用

[英]Web Api for OData V4 doesn't Work With HasKey

我將Web API 2.2用於odata v4,以下是模型:

[Table("User")]
public partial class User
{
    public User()
    {
        UserRoles = new HashSet<UserRole>();
    }

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

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

    public virtual ICollection<UserRole> UserRoles { get; set; }
}

[Table("UserRole")]
public partial class UserRole
{
    [Key]
    [Column(Order = 0)]
    [StringLength(50)]
    public string UserId { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(50)]
    public string RoleName { get; set; }

    public virtual User User { get; set; }
}

下面是WebApiConfig.cs代碼:

        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<User>("Users");
        builder.EntitySet<UserRole>("UserRoles");            
        config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
        config.AddODataQueryFilter();

到現在為止,一切正常。 但是,如果我從用戶角色定義中刪除了[Key]。 並使用代碼:

builder.EntitySet<UserRole>("UserRoles").EntityType.HasKey(r => new { r.UserId, r.RoleName });

當我在User上查詢時,我總是得到告訴UserRoles沒有鍵定義的錯誤。

我已經上載了源項目文件@ https://cloud.seafile.com/f/bdff340ec3/請檢查webapiconfig.cs User.cs UserRole.cs和web.config(用於數據庫連接字符串)。 當您訪問/ odata / Users?$ expand = UserRoles時,您將收到錯誤消息

只是改變

builder.EntitySet<UserRole>("UserRoles").EntityType.HasKey(r => new { r.UserId, r.RoleName });

EntityTypeConfiguration<UserRole> userRoleType=builder.EntitySet<UserRole>("UserRoles").EntityType;
userRoleType.HasKey(p=>p.UserId);
userRoleType.HasKey(p=>p.RoleName);

我也有同樣的問題。 這是我現在正在做的事情,尚未完成,所以我不知道是否還會有其他問題。

在我的模型中,我將UserId更改為Id,因此OData約定將其用作鍵,然后在我的dbcontext映射文件中使用:

public class DbLdapMap : EntityTypeConfiguration<DbLdap>
{
    public DbLdapMap()
    {
        ToTable("LDAP");

        //Primary Key
        HasKey(t => t.Id);

        // Properties
        Property(t => t.Id).HasColumnName("UserId");

    }
}

我真的很想將所有數據庫定義保留在Fluent API中。

我嘗試了您的項目,對我來說,odata元數據“ / odata / $ metadata”似乎還不錯。

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
  <edmx:DataServices>
    <Schema Namespace="WebApplication1.Models.OData" xmlns="http://docs.oasis-open.org/odata/ns/edm">
      <EntityType Name="User">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.String" Nullable="false" />
        <Property Name="Name" Type="Edm.String" Nullable="false" />
        <NavigationProperty Name="UserRoles" Type="Collection(WebApplication1.Models.OData.UserRole)" />
      </EntityType>
      <EntityType Name="UserRole">
        <Key>
          <PropertyRef Name="UserId" />
          <PropertyRef Name="RoleName" />
        </Key>
        <Property Name="UserId" Type="Edm.String" Nullable="false" />
        <Property Name="RoleName" Type="Edm.String" Nullable="false" />
        <NavigationProperty Name="User" Type="WebApplication1.Models.OData.User" />
      </EntityType>
    </Schema>
    <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
      <EntityContainer Name="Container">
        <EntitySet Name="Users" EntityType="WebApplication1.Models.OData.User">
          <NavigationPropertyBinding Path="UserRoles" Target="UserRoles" />
        </EntitySet>
        <EntitySet Name="UserRoles" EntityType="WebApplication1.Models.OData.UserRole">
          <NavigationPropertyBinding Path="User" Target="Users" />
        </EntitySet>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

問題在於UserRole在DB模型而不是OData EDM模型中沒有定義鍵。

如果刪除UserId和RoleName上的Key屬性,則DBModelBuilder不知道鍵是什么。

因此,您需要將以下代碼添加到不帶[Key]的Database.OnModelCreating中

modelBuilder.Entity<UserRole>()
                .HasKey(r => new { r.UserId, r.RoleName });

此外, 如果要支持對結果應用$ expand,則需要在操作上添加EnableQuery屬性

        [EnableQuery]
        public IQueryable<User> GetUsers()
        {
            var db = new Database();
            return db.Users;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM