【问题标题】:Ef core Odata Many-to-manyEf core Odata 多对多
【发布时间】:2020-07-14 16:11:30
【问题描述】:

我正在使用 ODATA 开发 Asp .Net Core 2.2 应用程序。我有测试应用程序来重现问题: 我无法通过 ODATA 请求带有链接表的链接实体

请求:/odata/Books?$expand=BookCategories

响应错误:

"Message": "The query specified in the URI is not valid. Property 'BookCategories' on type 'GameStorageView.Data.Book' is not a navigation property or complex property. Only navigation properties can be expanded.",
"ExceptionMessage": "Property 'BookCategories' on type 'GameStorageView.Data.Book' is not a navigation property or complex property. Only navigation properties can be expanded.",
"ExceptionType": "Microsoft.OData.ODataException",

型号:

 public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        public ICollection<BookCategory> BookCategories { get; set; }
    }

    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public ICollection<BookCategory> BookCategories { get; set; }
    }

    public class BookCategory
    {
        public int BookId { get; set; }
        public Book Book { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }

模型创建:

modelBuilder.Entity<BookCategory>()
                .HasKey(bc => new {bc.BookId, bc.CategoryId});
            modelBuilder.Entity<BookCategory>()
                .HasOne(bc => bc.Book)
                .WithMany(b => b.BookCategories)
                .HasForeignKey(bc => bc.BookId);
            modelBuilder.Entity<BookCategory>()
                .HasOne(bc => bc.Category)
                .WithMany(c => c.BookCategories)
                .HasForeignKey(bc => bc.CategoryId);

如何让它发挥作用? 如何通过 odata 请求多对多实体?

更新:

ODATA 已配置: .EntityType.Count().Filter().OrderBy().Expand(SelectExpandType.Allowed,10).Select().Page().Count();

linq 查询工作正常: context.Book.Include(c=&gt;c.BookCategories).ToArray()

【问题讨论】:

  • 能否请您显示添加/创建 Edm 模型的代码?
  • 我使用code-first和fluent api,OnModelCreating方法和模型你可以在上面看到)
  • 能否将测试应用发布到 GitHub 上?在这种情况下,我将能够在本地运行并尝试解决您的问题

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


【解决方案1】:

好的,我的解决方案不是最好的,但它有效:我们需要向它添加字段 Idunique constraint

public class BookCategory
    {
        public int Id { get; set; }
        public int BookId { get; set; }
        public Book Book { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }

modelBuilder.Entity<BookCategory>().HasAlternateKey(bc => bc.Id);

然后如果我们想通过 odata 加载链接实体,我们还需要将链接实体添加到 odata 路由。我使用扩展方法,所以它看起来像这样:

public static void RegisterOdataRoute<TEntity>(this ODataConventionModelBuilder builder, string name = null, bool pluralize = true) where TEntity : class
        {
            name = name ?? $"{typeof(TEntity).Name}";

            var names = pluralize ? $"{name}s" : name;

            OdataRoutes.Add(name, $"~/odata/{names}");
            builder.EntitySet<TEntity>(names).EntityType.Count().Filter().OrderBy().Expand(SelectExpandType.Allowed,10).Select().Page().Count();
        }
    }

app.UseOData(builder =>
            {
                builder.RegisterOdataRoute<BookCategory>();
                    ...

如果有人找到更好的解决方案,请告诉我!

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2018-12-07
    • 1970-01-01
    • 2018-06-02
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多