I am using C# and Entity Framework. I want to define a key for the SimulationParameter
object as a combination of three columns (Name, StudyId, SimulationId). I don't need an ID column, since the combination of those three elements will always be unique.
Simulation Parameter:
public class SimulationParameter : IAggregateRoot
{
public SimulationParameter()
{
}
public String SimulationId { get; set; }
public Guid StudyId { get; set; }
public Study Study { get; set; }
public String Name { get; set; }
public String Value { get; set; }
}
Simulation Parameter Mapping:
class SimulationParameterMap : EntityTypeConfiguration<SimulationParameter>
{
public SimulationParameterMap()
{
HasKey(e => new {SimulationId = e.SimulationId, Name = e.Name, StudyId = e.StudyId});
Property(e => e.SimulationId)
.IsRequired();
Property(e => e.Name)
.IsRequired();
Property(e => e.Value)
.IsRequired();
HasRequired(e => e.Study)
.WithMany(e => e.SimulationsParameters)
.HasForeignKey(s => s.StudyId);
ToTable("SimulationParameters");
}
}
Now, when I try to create the model, I have the following error:
EntityType 'SimulationParameter' has no key defined. Define the key for this EntityType.
SimulationParameters: EntityType: EntitySet 'SimulationParameters' is based on type 'SimulationParameter' that has no keys defined.
I am really clueless on why this is not valid...
Edit 1:
Based on a suggestion, I added the [Key]
attribute above the field in the model:
public class SimulationParameter : IAggregateRoot
{
public SimulationParameter()
{
}
[Key]
public String SimulationId { get; set; }
[Key]
public Guid StudyId { get; set; }
public Study Study { get; set; }
[Key]
public String Name { get; set; }
public String Value { get; set; }
}
And got a different error:
System.InvalidOperationException: Unable to determine composite primary key ordering for type 'SimulationParameter'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys.
Edit2
I was able to make it work doing the following:
public class SimulationParameter : IAggregateRoot
{
public SimulationParameter()
{
}
[Key, Column(Order=1)]
public String SimulationId { get; set; }
[Key, Column(Order = 2)]
public Guid StudyId { get; set; }
public Study Study { get; set; }
[Key, Column(Order = 3)]
public String Name { get; set; }
public String Value { get; set; }
}
It is still strange though that it isn't working using fluent, I'll keep looking and keep you posted.