Pergunta

I used the following class:

public class Point
{
    [Required]
    public double X { get; set; }
    [Required]
    public double Y { get; set; }
}

And the Model containing:

public List<Point> Vertices { get; set; }

But EF tries to create a separate table based on this message :

EntityType 'Point' has no key defined. Define the key for this EntityType.

I'll have large number of points for related model and I think it's not optimal solution to have a separate table. Is there a way to embed point array inside the table or am I thinking wrong?

Highly appreciate your time. Thanks.

Foi útil?

Solução

The following should work - not sure if I would recommend this approach (as I think a table for point would be ok) but having said that it did seem to work for me. So I use the VerticeDataText to store the point data in a string, so it is stored in the DB:

public class Vertice
{
    public int VerticeID { get; set; }

    [NotMapped]
    public Point[] Data
    {
        get
        {
            string[] rawInternalData = this.VerticeDataText.Split(';');
            Point[] dataResult = new Point[rawInternalData.Length / 2];
            int count = 0;
            rawInternalData.ToList().ForEach(p =>
            {
                var pairArray = p.Split(',');
                dataResult[count++] = new Point { X = double.Parse(pairArray[0]), Y = double.Parse(pairArray[1])};
            });
            return dataResult;
        }
        set
        {
            StringBuilder sb = new StringBuilder();
            value.ToList().ForEach(p => sb.AppendFormat("{0},{1};", p.X, p.Y));
            this.VerticeDataText = sb.ToString();
        }
    }

    public string VerticeDataText { get; set; }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top