【发布时间】: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=>c.BookCategories).ToArray()
【问题讨论】:
-
能否请您显示添加/创建 Edm 模型的代码?
-
我使用code-first和fluent api,OnModelCreating方法和模型你可以在上面看到)
-
能否将测试应用发布到 GitHub 上?在这种情况下,我将能够在本地运行并尝试解决您的问题
标签: c# asp.net .net entity-framework-core odata