7
model.cs

[Column(TypeName = "json")]
    public string application_role { get; set; }

Its MySQL, data type of particular column is json, and how to add it in a model class. I tried with DataAnnotations but getting error as

The specified type member 'application_role' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Linq Query to get data

context.sc_employee_details
                    .Where(e => e.user_name.Equals(userName))
                    .Select(o => o.application_role).SingleOrDefault();
5
  • Why not use it as string only? just serialize and deserialize ? Commented Nov 23, 2017 at 12:35
  • PraveenR You will need to serialize and deserialize json data as @Hey24sheep has indicated. I don't think you can handle json any other way. Commented Nov 23, 2017 at 12:42
  • @AbhishekMaurya removed DataAnnotations but still same error Commented Nov 23, 2017 at 12:47
  • @PraveenR I meant something like this newtonsoft.com/json/help/html/SerializingJSON.htm Commented Nov 23, 2017 at 12:51
  • @AbhishekMaurya I got it, some what like this? get { var ser = new JsonSerializer(); /// .. return ser.Deserialize<Car[]>(jr); } set { var ser = new JsonSerializer(); //... } Commented Nov 23, 2017 at 12:54

2 Answers 2

5

It might be a bit late, but EF on MySQL supports JSON format, here is announcement. Basically, you have to define an attribute like this one:

public JsonObject<string[]> Tags { get; set; } // Json storage

hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

2

You will do something like this

   private string _application_role;
   public string application_role 
   { 
       get{
            return JsonConvert.DeserializeObject<string>(_application_role)
         } 
       set{
           _application_role = JsonConvert.SerializeObject(value);
         } 
   }

Or if you do not want to edit your model then you could do something like this

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.