I am trying to store a matrix in a database using a code-first model with Entity Framework. I have created an object for each element in the matrix. I want to be able to search both rows and columns so I was hoping to have both my RowID
and ColID
as Key
. Using composite keys I am still getting the error:
"Trojan.Models.Matrix_Element: : EntityType 'Matrix_Element' has no key defined. Define the key for this EntityType.
default_Matrix: EntityType: EntitySet 'default_Matrix' is based on type 'Matrix_Element' that has no keys defined."
I am guessing that this is because there are duplicate values for RowID
and ColID
. It is a matrix so every pair (RowID, ColID)
is unique but I am guessing that because there are multiple entries per row and multiple entries per column it considers there being elements with the same PK.
Is there a way to identify an element ONLY by the pair of keys? Or am I forced to create an ElementID
attribute and use that as a PK?
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Trojan.Models
{
public class Matrix_Element
{
[Key, Column(Order = 0)]
public int RowID;
[Key, Column(Order = 1)]
public int ColID;
public int? cellValue;
public string R;
}
}