当我运行以下示例时,它会引发以下异常...
附加信息:实体“TestEntity”没有定义键。
我已经使用代码优先实体上下文配置了密钥...
modelBuilder.Entity<TestEntity>().HasKey(t => t.EntityID);
问题是什么?为什么 OData V4 不使用配置的密钥?
namespace WebApplication2
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapODataServiceRoute("odata", "odata", model: GetEDMModel());
}
private static IEdmModel GetEDMModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<TestEntity>("TestEntities");
return builder.GetEdmModel();
}
}
public class TestEntity
{
public int EntityID { get; set; }
public string Name { get; set; }
}
public partial class TestContext1 : DbContext
{
public TestContext1() : base("DB")
{
}
public DbSet<TestEntity> Entities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestEntity>().HasKey(t => t.EntityID);
}
}
}