views:

37

answers:

2

Hi all,

I'm in charge to migrate our own DAL to a solution based on Entity Framework 4 but, before I can do it, I need to be sure it's possible to translate all our "constructs" to this new technology.

One of the biggest issues I'm having is the possibility to read a field and build a custom type. Valid examples could be a bit mask saved in a BIGINT field, a list of mail addresses saved as a CSV list in a NVARCHAR field or an XML field containing aggregated data not worth to have their own table/entity. Basically the serialization mechanism is not fixed.

Let's take the classic "Address" example.

public class Address
{
    public string Street {get; set;}
    public string City {get; set;}
    public string Zip {get; set;}
    public string Country {get; set;}
}

and let's suppose we want to save it in an XML field using this template:

<address>
  <street>Abrahamsbergsvägen, 73</street>
  <city>Stockholm</city>
  <zip>16830</zip>
  <country>Sweden</country>
</address>

The question basically is: does exist a method to override how EF4 serializes and deserializes the content of a field mapped to a property of an entity?

+1  A: 

Entity Framework does NOT serializes or deserializes entities nor it controls how the serialization should take place in other layers or modules of your application.
What you need to do is to simply open your POCO(s) and annotate their Properties with appropriate attributes that will be taken into account at the time of serialization when you want to ship them to your other application layers.

Morteza Manavi
I didn't mean to serialize an entity toward other application layers but the value of a specific field
Kralizek
A: 

I found this solution. It's not as clean as I wished but it seems it's impossible to get anything better.

given this base entity,

public class Institute
{
    public int InstituteID { get; set; }
    public string Name { get; set; }
    // other properties omitted
}

I added in the database an XML field called Data containing some strings using this simple template

<values>
  <value>Value 1</value>
  <value>Value 2</value>
  <value>Value 3</value>
</values>

In the entity I added these properties and I mapped the database field "Data" to the property "DataRaw".

protected string DataRaw
{
    get
    {
        if (_Data == null)
            return _DataRaw;
        else
            return new XElement("values", from s in Data select new XElement("value", s)).ToString();
    }
    set
    {
        _DataRaw = value;
    }
}

private string _DataRaw;
private string[] _Data;

public string[] Data
{
    get
    {
        if (_Data == null)
        {
            _Data = (from elem in XDocument.Parse(_DataRaw).Root.Elements("value")
                     select elem.Value).ToArray();
        }
        return _Data;
    }

    set
    {
        _Data = value;
    }
}

This solution works. Here is the sample code:

class Program
{
    static void Main(string[] args)
    {
        var ctx = new ObjectContext("name=TestEntities");

        var institute = ctx.CreateObjectSet<Institute>().First();

        System.Console.WriteLine("{0}, {1}", institute.InstituteID, institute.Name);
        foreach (string data in institute.Data)
            System.Console.WriteLine("\t{0}", data);

        institute.Data = new string[] { 
            "New value 1",
            "New value 2",
            "New value 3"
        };

        ctx.SaveChanges();
    }
}

Does anyone have a better solution?

Kralizek