没有为多列上的键定义键

时间:2014-11-21 19:16:20

标签: c# entity-framework ef-fluent-api

我正在使用C#和Entity Framework。我想将SimulationParameter对象的键定义为三列(Name,StudyId,SimulationId)的组合。我不需要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.

EDIT2

我能够让它发挥作用:

  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; }



    }

虽然它不能流利使用,但仍然很奇怪,我会继续寻找并让你发布。

1 个答案:

答案 0 :(得分:1)

您不能将其他类中的属性用作主键:e.Study.IdSimulationParameter在数据库中也应该有一个StudyId(或类似的东西)。只需将此字段放入课程中并使其成为关键部分:

public class SimulationParameter : IAggregateRoot
{
    public SimulationParameter()
    {
    }

    public String SimulationId { get; set; }

    [ForeignKey("Study")]
    public int StudyId { get; set; }
    public Study Study { get; set; }

    public String Name { get; set; }
    public String Value { get; set; }            
}

或者,通过流畅的映射:

HasRequired(e => e.Study)
.WithMany(e => e.SimulationsParameters)
.HasForeignKey(s => s.StudyId);