using BooksODataService.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace BooksODataService.Controllers
{
public class BooksController : ODataController
{
private readonly BooksContext _booksContext;
public BooksController(BooksContext booksContext)
{
_booksContext = booksContext;
}
\[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)\]
public IQueryable<Book> Get(ODataQueryOptions options)
{
ODataValidationSettings settings = new ODataValidationSettings()
{
MaxExpansionDepth =
};
options.Validate(settings);
var books = \_booksContext.Books.Include(b => b.Chapters);
return books;
}
\[EnableQuery()\]
public SingleResult<Book> Get(\[FromODataUri\] int key)
{
return SingleResult.Create(\_booksContext.Books.Where(b => b.Id == key));
}
}
复制
}
using BooksODataService.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BooksODataService.Controllers
{
public class ChaptersController : ODataController
{
private readonly BooksContext _booksContext;
public ChaptersController(BooksContext booksContext)
{
_booksContext = booksContext;
}
public IQueryable<BookChapter> Get() =>
\_booksContext.Chapters.Include(c => c.Book);
\[EnableQuery\]
public SingleResult<BookChapter> Get(\[FromODataUri\] int key) =>
SingleResult.Create(\_booksContext.Chapters.Where(c => c.Id == key));
}
复制
}
using BooksODataService.Models;
using BooksODataService.Services;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
// OData Beta 1 docu http://odata.github.io/WebApi/#14-01-netcore-beta1
namespace BooksODataService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<CreateBooksService>();
services.AddDbContext<BooksContext>(options =>
{
string connString = Configuration.
GetConnectionString("BooksConnection");
options.UseSqlServer(connString);
});
services.AddOData();
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, CreateBooksService sampleBooks)
{
sampleBooks.CreateDatabase();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Book>("Books");
builder.EntitySet<BookChapter>("Chapters");
app.UseMvc(routeBuilder =>
{
routeBuilder.MapODataServiceRoute("ODataRoute",
"odata", builder.GetEdmModel());
routeBuilder.EnableDependencyInjection(); // workaround for Beta 1
});
}
}
复制
}
代码下载:https://files.cnblogs.com/files/Music/ODataSample-In-ASPNET-Core.rar
手机扫一扫
移动阅读更方便
你可能感兴趣的文章