Ahmad Kabakibi
Ahmad Kabakibi

Reputation: 21

Odata v4 ODataModelBuilder relations builder

i'm working to implement web api using OData v4 my database structure is seperate tables the relations between my tables should be represent inside Enitiy Framework i had implement my EF model for my database structuer as following:

ODataModelBuilder builder = new ODataConventionModelBuilder();
   builder.EntitySet<tabl1>("tabl1").EntityType.HasKey(p => p.ID);
   builder.EntitySet<tabl2>("tabl2").EntityType.HasKey(p => p.ID);
   builder.EntitySet<tabl3>("tabl3").EntityType.HasKey(p => p.ID);
   builder.EntitySet<tabl4>("tabl4").EntityType.HasKey(p => p.ID);
   config.MapODataServiceRoute(
            routeName: "ODataroute",
            routePrefix: "api",
            model: builder.GetEdmModel());

how can i implement relationship between my tables so i can be able to use query like this or if i can use normal linq Query action from my controller but it's not working with me
http://localhost:13193/api/table1?$expand=table2

Upvotes: 1

Views: 385

Answers (1)

Jan Hommes
Jan Hommes

Reputation: 5152

Don't know which EF technic you are using. If you are using code-first you just need to set the relation inside you model definition. The following model builds a 1 : n relation between tabl1 and tabl2:

public class tabl1
{
    public int Id { get; set; }
    public virtual tabl2 tabl2 { get; set; }
}

public class tabl2
{
    public int Id { get; set; }
    public virtual ICollection<tabl2> tabl2 { get; set; }
}

Upvotes: 1

Related Questions