首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OData抱怨启用camelcasing时缺少id属性。

OData抱怨启用camelcasing时缺少id属性。
EN

Stack Overflow用户
提问于 2016-09-01 10:36:18
回答 1查看 1.2K关注 0票数 2

我想让骆驼套我的数据结果。所以我加入了EnableLowerCamelCase。但是,在启用之后,当我调用时会得到以下错误消息:

代码语言:javascript
复制
http://localhost/odata/Users

类型'Core.DomainModel.User Nullable=True‘的EDM实例缺少属性'id’。

在我EnableLowerCamelCase之前,一切都正常工作,如果我删除它,它会再次工作。此外,错误信息也相当混乱。它说用户丢失了'id‘属性。这不可能是真的。因为我把“Id”定义为钥匙。

代码语言:javascript
复制
var builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase();

var users = builder.EntitySet<User>(nameof(UsersController).Replace("Controller", string.Empty));
users.EntityType.HasKey(x => x.Id); // <--- id property

builder.GetEdmModel();

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2018-12-14 22:42:46

我解决这个问题的方法是从EDM模型中删除实体键声明,并在模型本身中指定它,这样我的edm看起来就像‘

代码语言:javascript
复制
var builder = new ODataConventionModelBuilder(serviceProvider);
builder.EnableLowerCamelCase();
var subscriptionSet = builder.EntitySet<SubscriptionDTO>("Subscriptions");
  subscriptionSet.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

  // subscriptionSet.EntityType.HasKey(s => s.Id);
  //subscriptionSet.EntityType.EntityType.Property(s => s.Id).IsOptional();`

在模型中,使用DataAnnotations标识密钥:

代码语言:javascript
复制
public class BaseModel
  {
    [Key]
    public Guid? Id {get; set;}
    public Guid? TenantId {get; set;}
    public string Type {get; set;}
    public bool Active {get; set;} = true;

    public BaseModel() {
        this.Id = System.Guid.NewGuid();
    }

然后按照惯例将DTO与Automapper一起使用:

代码语言:javascript
复制
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]

    public class BaseDTO
    {
        public Guid? Id {get; set;}
        public Guid? TenantId {get; set;}
        public string Type {get; set;}
        public bool Active {get; set;} = true;

        public BaseDTO() {
            this.Id = System.Guid.NewGuid();
        }

    }

 [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
    public class SubscriptionDTO: BaseDTO
    {
        [JsonProperty("email")]
        public string Email {get; set;}

        public SubscriptionDTO(): base() {
           this.Type = "subscription";
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39269261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档