c# OData V4,EntityFramework 6,缺少属性
c# OData V4, EntityFramework 6, missing properties
我正在尝试将在 oData V3 上运行良好的服务迁移到 V4。
我正在使用 EntityFramework 6 从我的数据库生成代码。这是生成的代码:
namespace MyEntities
{
using System;
public partial class Civilite
{
public System.Guid pkCivilite { get; set; }
public string civilite { get; set; }
}
}
这是我的 WebApiConfig :
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
ODataModelBuilder builder = new ODataModelBuilder();
builder.Namespace = "DataService";
builder.ContainerName = "Container";
var civiliteSet = builder.EntitySet<MyEntities.Civilite>("Civilites");
civiliteSet.EntityType.HasKey(entity => entity.pkCivilite);
config.MapODataServiceRoute(
routeName: "odata",
routePrefix: null,
model: builder.GetEdmModel());
config.EnsureInitialized();
}
}
这是我的控制器:
public class CivilitesController : ODataController
{
MyEntities.Container db = new MyEntities.Container();
private bool CiviliteExists(Guid key)
{
return db.CiviliteSet.Any(c => c.pkCivilite == key);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<MyEntities.Civilite> Get()
{
return db.CiviliteSet;
}
[EnableQuery]
public SingleResult<MyEntities.Civilite> Get([FromODataUri] Guid key)
{
IQueryable<MyEntities.Civilite> result = db.CiviliteSet.Where(p => p.pkCivilite == key);
return SingleResult.Create(result);
}
public async Task<IHttpActionResult> Post(MyEntities.Civilite civilite)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.CiviliteSet.Add(civilite);
await db.SaveChangesAsync();
return Created(civilite);
}
但元数据不return 属性 "civilite" 类型的字符串。它只是 return 类型为 Guid 的主键 "pkCivilite"。
<?xml version="1.0" encoding="ISO-8859-1"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="DataService">
<EntityType Name="Civilite">
<Key>
<PropertyRef Name="pkCivilite"/>
</Key>
<Property Name="pkCivilite" Nullable="false" Type="Edm.Guid"/>
</EntityType>
<EntityContainer Name="Container">
<EntitySet Name="Civilites" EntityType="DataService.Civilite"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
我尝试将 [DataMember] 放在 属性 "civilite" 上,但没有成功。
客户请求工作。我得到了所有记录,但只有 "pkCivilite" 属性。
我究竟做错了什么 ?感谢您的帮助。
我的错误是使用 ODataModelBuilder
而不是 ODataConventionModelBuilder
。
所以我需要写:
ODataModelBuilder builder = new ODataConventionModelBuilder();
我正在尝试将在 oData V3 上运行良好的服务迁移到 V4。 我正在使用 EntityFramework 6 从我的数据库生成代码。这是生成的代码:
namespace MyEntities
{
using System;
public partial class Civilite
{
public System.Guid pkCivilite { get; set; }
public string civilite { get; set; }
}
}
这是我的 WebApiConfig :
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
ODataModelBuilder builder = new ODataModelBuilder();
builder.Namespace = "DataService";
builder.ContainerName = "Container";
var civiliteSet = builder.EntitySet<MyEntities.Civilite>("Civilites");
civiliteSet.EntityType.HasKey(entity => entity.pkCivilite);
config.MapODataServiceRoute(
routeName: "odata",
routePrefix: null,
model: builder.GetEdmModel());
config.EnsureInitialized();
}
}
这是我的控制器:
public class CivilitesController : ODataController
{
MyEntities.Container db = new MyEntities.Container();
private bool CiviliteExists(Guid key)
{
return db.CiviliteSet.Any(c => c.pkCivilite == key);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<MyEntities.Civilite> Get()
{
return db.CiviliteSet;
}
[EnableQuery]
public SingleResult<MyEntities.Civilite> Get([FromODataUri] Guid key)
{
IQueryable<MyEntities.Civilite> result = db.CiviliteSet.Where(p => p.pkCivilite == key);
return SingleResult.Create(result);
}
public async Task<IHttpActionResult> Post(MyEntities.Civilite civilite)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.CiviliteSet.Add(civilite);
await db.SaveChangesAsync();
return Created(civilite);
}
但元数据不return 属性 "civilite" 类型的字符串。它只是 return 类型为 Guid 的主键 "pkCivilite"。
<?xml version="1.0" encoding="ISO-8859-1"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="DataService">
<EntityType Name="Civilite">
<Key>
<PropertyRef Name="pkCivilite"/>
</Key>
<Property Name="pkCivilite" Nullable="false" Type="Edm.Guid"/>
</EntityType>
<EntityContainer Name="Container">
<EntitySet Name="Civilites" EntityType="DataService.Civilite"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
我尝试将 [DataMember] 放在 属性 "civilite" 上,但没有成功。 客户请求工作。我得到了所有记录,但只有 "pkCivilite" 属性。 我究竟做错了什么 ?感谢您的帮助。
我的错误是使用 ODataModelBuilder
而不是 ODataConventionModelBuilder
。
所以我需要写:
ODataModelBuilder builder = new ODataConventionModelBuilder();