ASP.Net Core + EF + OData V4 Core Beta 2 [英] ASP.Net Core + EF + OData V4 Core Beta 2
问题描述
在配置了EF的ASP.NET Core上有一个相当基本的Web API项目,该项目可以正常使用Web API.我正在关注此转换为使用Odata的文章,我不太明白它是否可以正常工作.
Have a fairly basic Web API project on ASP.NET Core configured with EF which was working okay with Web API. I was following this article to convert to use Odata and I can't quite get it to work.
我有一个名为customer的父对象,带有2个子对象:Addresss和Person.我已经为数据库设置了种子,因此我可以看到那里有数据,并且Odata端点看起来不错,因为当您启动项目时,它会显示实体,而odata/$ metadata会按预期显示EDM结构.
I have a parent object called customer with 2x child objects: Addresses and Person. I have seeded the database so I can see there is data there, and the Odata endpoint looks good because when you start the project it displays the entities and odata/$metadata displays the EDM structure as expected.
我目前唯一的问题是,当我导航到一个URL(例如/odata/customers)时,会收到一个空白屏幕.在邮递员中,它返回404.
The only issue I have currently is that when I navigate to a URL, such as /odata/customers, I receive a blank screen. In postman it returns 404.
我已经浏览了Lucas的示例项目(我所关注的文章),并在网上进行了相当多的研究,看不到我在做什么错.
I've combed through the example project from Lucas (the article I was following) and reseached a fair bit online and can't quite see what I'm doing wrong.
我确定这很简单/很傻,但是欢迎任何有建设性的指导意见:)
I'm sure it's something simple/silly, but any constructive guidance welcomed :)
**编辑**为简化起见(并根据反馈)删除了其他代码.让我知道是否需要其他信息.
** Edit ** Removed additional code for simplicity (and based on feedback). Let me know if additional information is required.
干杯
亚当
文件路径:Odata \ BookingsModelBuilder.cs
File Path: Odata\BookingsModelBuilder.cs
using System;
using Microsoft.AspNet.OData.Builder;
using Microsoft.OData.Edm;
using Bookings_Server.OData.Models;
namespace Bookings_Server
{
public class BookingsModelBuilder
{
public IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
builder.EntitySet<Address>("addresses")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select();// Allow for the $select Command;
builder.EntitySet<Customer>("customers")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select();// Allow for the $select Command;
builder.EntitySet<Person>("people")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select();// Allow for the $select Command;
return builder.GetEdmModel();
}
}
}
文件路径:EF \ DataContext.CS
File path: EF\DataContext.CS
using Microsoft.EntityFrameworkCore;
using Bookings_Server.OData.Models;
namespace Bookings_Server.EF
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public DbSet<Address> Addresses { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<Tenant> Tenants { get; set; }
}
}
文件路径:Controllers \ CustomersController.cs
File path: Controllers\CustomersController.cs
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Bookings_Server.EF;
using Bookings_Server.OData.Models;
using Microsoft.AspNet.OData;
namespace Bookings_Server.OData.Controllers
{
[Produces("application/json")]
public class CustomersController : ODataController
{
private readonly DataContext _context;
public CustomersController(DataContext context)
{
_context = context;
}
// GET: odata/customers
[EnableQuery(PageSize = 20)]
public IQueryable<Customer> Get() => _context.Customers.AsQueryable();
/*
public IActionResult Get()
{
return Ok(_context.Customers.AsQueryable());
}
*/
}
}
文件路径:startup.cs
File path: startup.cs
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNet.OData.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Bookings_Server
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, BookingsModelBuilder BookingsModelBuilder)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("cors");
// app.UseMvc();
// Added for Odata config
app.UseMvc(routeBuilder =>
{
routeBuilder.MapODataServiceRoute("ODataRoutes", "odata", BookingsModelBuilder.GetEdmModel(app.ApplicationServices));
});
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("cors", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}
));
var connection = @"Server=(localdb)\mssqllocaldb;Database=BookingsDB;Trusted_Connection=True;";
services.AddDbContext<EF.DataContext>(options => options.UseSqlServer(connection));
// Add OData configuration
services.AddOData();
services.AddTransient<BookingsModelBuilder>();
services.AddMvc().AddJsonOptions(opt =>
{
opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
}
}
}
推荐答案
好.解决这个问题.最后有点愚蠢.我在CustomerContoller.cs上缺少装饰器
Ok. Worked this out. It was something silly in the end. I was missing the decorator on the CustomerContoller.cs
[ODataRoute("customers")]
和名称空间:
using Microsoft.AspNet.OData.Routing;
之后,一切开始正常运行.
After that everything started working fine.
// GET: odata/customers
[ODataRoute("customers")]
[EnableQuery(PageSize = 20)]
public IQueryable<Customer> Get() => _context.Customers.AsQueryable();
其他信息: http://odata.github.io/WebApi /03-03-attrribute-routing/
这篇关于ASP.Net Core + EF + OData V4 Core Beta 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!