如何使用Entity Framework Code-First方法将double []数组存储到数据库中
如何使用Entity Framework Code-First将数组的数组存储到数据库中?对现有的代码和架构设计没有影响?
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?
我已经看过数据注释和流畅的API,我也考虑将双数组转换成字符串,并将该字节存储在数据库的自己的列中。
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.
我无法访问 public double [] Data {get;组; }
属性与Fluent API,然后我得到的错误消息是:
I cannot access the public double[] Data { get; set; }
property with Fluent API, the error message I then get is:
类型
double []
必须是不可为空的值类型才能使用
作为参数'T'。
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.
感谢大家的投入,由于您的帮助,我能够追踪解决这个问题的最佳方法。哪个是:
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());
}
}
感谢这些stackoverflow帖子:
String to Doubles数组和
将数组组合成字符串
Thanks to these stackoverflow posts: String to Doubles array and Array of Doubles to a String