R
R
Roman Koff2015-12-17 16:52:01
ASP.NET
Roman Koff, 2015-12-17 16:52:01

How to describe a custom data type so that EF can properly serialize/deserialize it?

There is a model like this:

public class Pub
{
   [Key]
   public int Id { get; set; }
   public string Name { get; set; }
   public StringList Items { get; set; } // <--
}

StringList class - contains a Value property that serializes/deserializes it to/from string.
public class StringList
{
   ...
   public string Value
   {
      get { return this.ToString(); }
      set { this.FillFromString(value); }
   }
}

How to explain to EF that when filling the model from the database, you need to set the value to model.Items.Value, and when reading from the model to the database, retrieve the value from model.Items.Value?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rasim Keita, 2016-02-01
@keyros

try to separate the Model from any logic,
personally in my opinion it would be very good
and convert from / to string / string, entrust someone else, you should not force the model to do this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question