C# と Entity Framework を使用しています。SimulationParameter
オブジェクトのキーを 3 つの列 (Name、StudyId、SimulationId) の組み合わせとして定義したいと考えています。これら 3 つの要素の組み合わせは常に一意であるため、ID 列は必要ありません。
シミュレーション パラメータ:
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; }
}
シミュレーション パラメータ マッピング:
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");
}
}
今、モデルを作成しようとすると、次のエラーが発生します。
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.
なぜこれが有効ではないのか、私には本当にわかりません...
編集1:
提案に基づいて[Key]
、モデルのフィールドの上に属性を追加しました。
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; }
}
そして、別のエラーが発生しました:
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.
編集2
次のようにして動作させることができました。
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; }
}
流暢に動作しないのはまだ奇妙ですが、私は探し続け、あなたに投稿し続けます.