Skip to content

Model Bound Attributes

Maxim edited this page Apr 16, 2019 · 6 revisions

About model bound attributes
Support Page, Count, OrderBy, Filter, Select, Expand attributes.

Model Bound attribute APIs

var optionsBuilder = new DbContextOptionsBuilder<Model.OrderContext>();
optionsBuilder = optionsBuilder.UseSqlServer(@"Server=.\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;");

//Create adapter data access, where OrderContext your DbContext
var dataAdapter = new OeEfCoreDataAdapter<Model.OrderContext>(optionsBuilder.Options);
//Build OData Edm Model
EdmModel edmModel = dataAdapter.BuildEdmModel();
//Create model bound provider from attributes
var modelBoundAttributeBuilder = new Query.Builder.OeModelBoundAttributeBuilder(edmModel);
OeModelBoundProvider modelBoundProvider = modelBoundAttributeBuilder.BuildProvider();
//Create query parser
var parser = new OeParser(new Uri("http://dummy"), edmModel, modelBoundProvider);
//Query
var uri = new Uri("http://dummy/Orders");
//The result of the query
var response = new MemoryStream();
//Execute query
await parser.ExecuteGetAsync(uri, OeRequestHeaders.JsonDefault, response, CancellationToken.None);

Model Bound Fluent APIs

var optionsBuilder = new DbContextOptionsBuilder<Model.OrderContext>();
optionsBuilder = optionsBuilder.UseSqlServer(@"Server=.\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;");

//Create adapter data access, where OrderContext your DbContext
var dataAdapter = new OeEfCoreDataAdapter<Model.OrderContext>(optionsBuilder.Options);
//Build OData Edm Model
EdmModel edmModel = dataAdapter.BuildEdmModel();

//Create model bound provider
var modelBoundFluentBuilder = new Query.Builder.OeModelBoundFluentBuilder(edmModel);
modelBoundFluentBuilder.EntitySet<Model.Customer>("Customers").EntityType
    .Expand(SelectExpandType.Disabled, "AltOrders")
    .Expand(SelectExpandType.Automatic, "Orders")
    .Property(c => c.Orders).Count(QueryOptionSetting.Disabled);

modelBoundFluentBuilder.EntitySet<Model.Order>("Orders").EntityType
    .Count(QueryOptionSetting.Allowed)
    .Expand(SelectExpandType.Automatic, "Customer", "Items")
    .Page(2, 1)
    .Select(SelectExpandType.Automatic, "Name", "Date", "Status")
    .Property(o => o.Customer).Select(SelectExpandType.Automatic, "Name", "Sex")
    .Property("Items").Count(QueryOptionSetting.Allowed).Page(2, 1).OrderBy(QueryOptionSetting.Allowed, "Id");

modelBoundFluentBuilder.EntitySet<Model.OrderItem>("OrderItems").EntityType
    .Count(QueryOptionSetting.Disabled)
    .Filter(QueryOptionSetting.Disabled, "Id")
    .OrderBy(QueryOptionSetting.Disabled)
    .Select(SelectExpandType.Disabled, "Id", "OrderId");
OeModelBoundProvider modelBoundProvider = modelBoundFluentBuilder.BuildProvider();

//Create query parser
var parser = new OeParser(new Uri("http://dummy"), edmModel, modelBoundProvider);
//Query
var uri = new Uri("http://dummy/Orders");
//The result of the query
var response = new MemoryStream();
//Execute query
await parser.ExecuteGetAsync(uri, OeRequestHeaders.JsonDefault, response, CancellationToken.None);