Ric*_*nia 1 .net c# partial-classes
我使用的是 Entity Framework Core,生成的类有自己的属性,即
数据模型.Agent.cs
public partial class Agent {
public virtual decimal Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
但我需要其他属性,所以我在另一个文件中声明它们:
代理.cs
public partial class Agent
{
[NotMapped]
public dynamic Custom { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
问题是Agent.cs是在DataModel.Agent.cs之前编译的,所以编译器按照这个顺序生成属性:Custom、Id、Name,结果JSON很奇怪。
我希望它是:ID、名称、自定义。换句话说,我总是希望 DataModel 类排在第一位。
编辑:澄清一下,唯一的目标是通过始终将 Id 放在首位来使 JSON 更漂亮,这是一种非常常见的模式。这对应用程序的工作方式绝对没有影响。
有没有办法强制编译器始终首先编译其中一个文件?
好吧,如果使用 json.net,您真的不应该指望 JSON 属性顺序
public class Account
{
public string EmailAddress { get; set; }
// appear last
[JsonProperty(Order = 1)]
public bool Deleted { get; set; }
[JsonProperty(Order = 2)]
public DateTime DeletedDate { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime UpdatedDate { get; set; }
// appear first
[JsonProperty(Order = -2)]
public string FullName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
http://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm