Search code examples
c#.netentity-frameworkef-code-firstfluent-interface

How to store double[] array to database with Entity Framework Code-First approach


How can I store an array of doubles to database using Entity Framework Code-First with no impact on the existing code and architecture design?

I've looked at Data Annotation and Fluent API, I've also considered converting the double array to a string of bytes and store that byte to the database in it own column.

I cannot access the public double[] Data { get; set; } property with Fluent API, the error message I then get is:

The type double[] must be a non-nullable value type in order to use it as parameter 'T'.

The class where Data is stored is successfully stored in the database, and the relationships to this class. I'm only missing the Data column.


Solution

  • Thank you all for your inputs, due to your help I was able to track down the best way to solve this. Which is:

     public string InternalData { get; set; }
     public double[] Data
     {
        get
        {
            return Array.ConvertAll(InternalData.Split(';'), Double.Parse);                
        }
        set
        {
            _data = value;
            InternalData = String.Join(";", _data.Select(p => p.ToString()).ToArray());
        }
     }
    

    Thanks to these stackoverflow posts: String to Doubles array and Array of Doubles to a String