我想让骆驼套我的数据结果。所以我加入了EnableLowerCamelCase。但是,在启用之后,当我调用时会得到以下错误消息:
http://localhost/odata/Users类型'Core.DomainModel.User Nullable=True‘的EDM实例缺少属性'id’。
在我EnableLowerCamelCase之前,一切都正常工作,如果我删除它,它会再次工作。此外,错误信息也相当混乱。它说用户丢失了'id‘属性。这不可能是真的。因为我把“Id”定义为钥匙。
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();我做错了什么?
发布于 2018-12-14 22:42:46
我解决这个问题的方法是从EDM模型中删除实体键声明,并在模型本身中指定它,这样我的edm看起来就像‘
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标识密钥:
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一起使用:
[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";
}
}https://stackoverflow.com/questions/39269261
复制相似问题