Pra*_*n R 5 c# asp.net entity-framework asp.net-web-api2
model.cs
[Column(TypeName = "json")]
public string application_role { get; set; }
Run Code Online (Sandbox Code Playgroud)
它的 MySQL,特定列的数据类型是json,以及如何将其添加到模型类中。我尝试使用 DataAnnotations 但出现错误
The specified type member 'application_role' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Run Code Online (Sandbox Code Playgroud)
Linq Query 获取数据
context.sc_employee_details
.Where(e => e.user_name.Equals(userName))
.Select(o => o.application_role).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
可能有点晚了,但是 MySQL 上的 EF 支持 JSON 格式,这里是公告。基本上,您必须定义一个如下所示的属性:
public JsonObject<string[]> Tags { get; set; } // Json storage
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
你会做这样的事情
private string _application_role;
public string application_role
{
get{
return JsonConvert.DeserializeObject<string>(_application_role)
}
set{
_application_role = JsonConvert.SerializeObject(value);
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想编辑模型,那么您可以这样做
var myRole = context.sc_employee_details
.Where(e => e.user_name.Equals(userName))
.Select(o => o.application_role).SingleOrDefault();
if(myRole != null){
var desRole = JsonConvert.DeserializeObject<string>(myRole);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7930 次 |
| 最近记录: |