对OData中实体的属性进行别名/重命名
本文关键字:别名 重命名 属性 OData 实体 | 更新日期: 2023-09-27 18:10:45
使用ODataConventionModelBuilder
及其EntitySet<>
功能,是否可以重命名实体集上的属性名称?
假设我有一个实体集类型Foo
。它有两种性质,Bar
和Baz
。但是,在我的OData模型中,我希望将属性分别命名为Jack
和Jane
。我可以这样做吗?
我希望是这样的:
var builder = new ODataConventionModelBuilder { Namespace = "Blah" };
var foo = builder.EntitySet<Foo>("Foo");
foo.AliasProperty(f => f.Bar, "Jack");
foo.AliasProperty(f => f.Baz, "Jane");
到目前为止,我还没有找到这样做的东西
您可以使用DataContract/DataMember来声明性地完成此操作,例如
[DataContract]
public class Foo
{
[DataMember]
public Id { get; set;}
[DataMember(Name = "Jack"]
public string Bar { get; set;}
[DataMember(Name = "Jane"]
public string Baz { get; set;}
public int Fizz { get; set; }
[NotMapped]
public bool Buzz { get; set;
}
任何没有属性或带有[NotMapped]的都不会出现在OData模型中。
- 可用于任何属性类型,包括导航
- 在元数据中保留类的属性顺序,更改
odatacontiononmodelbuilder发生在模型构建之前,所以
你倾向于首先看到你的属性
缺点/strong>
- 使用NotMapped属性会干扰你的数据库映射时,有时使用约定表示没有属性是有用的意味着它不会在OData模型
这在官方文档http://odata.github.io/WebApi/#02-04-convention-model-builder中也有描述,以及其他属性,如[ConcurrencyCheck]和[ComplexType]
可以。基于https://github.com/OData/ODataSamples/blob/master/WebApiClassic/ODataModelAliasingSample/ODataModelAliasingSample/Program.cs:
var builder = new ODataConventionModelBuilder { Namespace = "Blah" };
var foo = builder.EntitySet<Foo>("Foo");
foo.Property(f => f.Bar).Name = "Jack";
foo.Property(f => f.Baz).Name = "Jane";
我一直无法将此用于导航属性。