[英]How to use Json data type in C# Entity Framework model?
model.cs
[Column(TypeName = "json")]
public string application_role { get; set; }
它的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.
Linq查询获取数据
context.sc_employee_details
.Where(e => e.user_name.Equals(userName))
.Select(o => o.application_role).SingleOrDefault();
你会做这样的事情
private string _application_role;
public string application_role
{
get{
return JsonConvert.DeserializeObject<string>(_application_role)
}
set{
_application_role = JsonConvert.SerializeObject(value);
}
}
或者,如果您不想编辑模型,那么您可以执行此类操作
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);
}
可能有点晚了,但MySQL上的EF支持JSON格式,这里是公告 。 基本上,你必须定义一个像这样的属性:
public JsonObject<string[]> Tags { get; set; } // Json storage
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.