简体   繁体   中英

OData and EF 6 Code first navigation property issue

I have next tables created via EF6 Code first. 模型

I generated 2 OData controllers to EF via wizard. in comments I need add model builder initialization I did it like in comments

  // Web API configuration and services

        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<SchoolChildrens>("SchoolChildrens");
        builder.EntitySet<AttendanceBook>("AttendanceBookSet");
        builder.EntitySet<FoodBudgetIncome>("FoodBudgetIncomeSet");

        builder.EntitySet<MenuOptions>("MenuOptions");
        builder.EntitySet<DishType>("DishTypeSet");
        builder.EntitySet<MenuDetails>("MenuDetailsSet");

        builder.EntitySet<DishType>("DishTypes");
        builder.EntitySet<MenuOptions>("MenuOptionsSet");

but I am getting an error

Cannot automatically bind the navigation property 'DishType' on entity type 'SchoolMeals.Data.MenuOptions' for the source entity set 'MenuOptions' because there are two or more matching target entity sets. The matching entity sets are: DishTypeSet, DishTypes.

If I remove

builder.EntitySet("DishTypes"); builder.EntitySet("MenuOptionsSet");

I can't use my 2 controller public class DishTypesController : ODataController

The problem is caused by the fact you have mapped DishType and MenuOption to two seperate entities :-

builder.EntitySet<DishType>("DishTypes");
builder.EntitySet<DishType>("DishTypeSet");
builder.EntitySet<MenuOption>("MenuOption");
builder.EntitySet<MenuOption>("MenuOptionSet");

The error message shows that the convention model builder can't automatically bind the navigation property "'DishType" to an entity set, Because of there are two entity sets. In this situation, you should use the Fluent APIs to explicitly bind the navigation property to an entity set. The APIs that you can use as follows in NavigationSourceConfigurationOfTEntityType :

  • HasManyBinding
  • HasRequiredBinding
  • HasOptionBinding ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM