0

Let say I have general table of records (I'm using EF code first) and there can be many different types of details that can be used with this table:

class Record{
int Id,
int TypeOfDetails, // int enum Type1, Type2
string Details, //string with json content
string Attribute 1,
string Atribute 2,
...
}

class DetailsType1{
  string InternalAtribute1,
  ....
}

I decided to use Json to serialize Details objects into database field. This allows to flexible work with different Details without changing DB schema.

Working with one records is easy, I know type of the Details object, so I can serialize and deserialize.

But how to efficiently retrieve multiple records and be able to reference details in in MVC ? Let say I do this:

var results = await context.Record.Where(..).ToAsyncList();

I would want to be able to do this in my .cshtml:

@Html.DisplayFor(modelItem => item.Record.Details.InternalAtribute1)

Essentially it would be doing deserialization on the fly..

Alternatively if something like that is possible in AngularJS it would also be helpful.

Let me know if I make myself clear with my question.

3
  • It's not clear what you want to achieve. Can you explain in more details what do you want to save in Details field? Commented Mar 25, 2015 at 12:41
  • I want to save object DetailsType1 as json. There might be also DetailsType2 and DetailsType3, so I could used them depending on situation.
    – drakkar
    Commented Mar 25, 2015 at 12:59
  • To illustrate my first idea is to: 1. add Object DetailsExpanded field to Record class that is [NoMapped] so to not go to db. Then create method that will deserialize Details into this Property... Not very elegant nor effective, as I need to execute this method in loop..
    – drakkar
    Commented Mar 25, 2015 at 13:11

1 Answer 1

0

I will answer my questions. The best I figure out was this:

    class Record{
    int Id { get; set; }
    int TypeOfDetails { get; set; } // int enum Type1, Type2
    string JsonDetails { get; set; } //string with json content
    string Attribute1 { get; set; }
    string Atribute2 { get; set; }
    ...
    [NotMapped]
    public JObject Details { get; set; }

    public void PopulateDetails()
    {
        Details = JObject.Parse(JsonDetails);
    }    
}

You still need to execute PopulateDetails on all query results, I'm doing it in foreach loop. Then in CSHTML you can do this:

@item.Details["InternalAtribute1"])

Your internal Json object's details can be displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.