3

我正在使用 C# 和实体框架。我想将对象的键定义为SimulationParameter三列(名称、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.

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



    }

虽然它不能使用流利的方式工作,但仍然很奇怪,我会继续寻找并随时通知您。

4

1 回答 1

1

You can't use a property in another class as primary key: e.Study.Id. SimulationParameter should also have a StudyId (or something like it) in the database. Just take this field into the class and make it part of the key:

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

Or, with fluent mapping:

HasRequired(e => e.Study)
.WithMany(e => e.SimulationsParameters)
.HasForeignKey(s => s.StudyId);
于 2014-11-21T19:35:20.390 回答