我遇到了错误:
关系约束中的从属角色和主要角色中的属性数必须相同 在datacontext初始化。
我的数据库包含几个表,AttachedMeter表具有由
组成的主键实体:
public class AttachedMeter : IEquatable<AttachedMeter>
{
public int GatewayId { get; set; }
public DateTime Timestamp { get; set; }
public virtual Configuration Configuration { get; set; }
public int MeterId { get; set; }
public virtual Meter Meter { get; set; }
}
public class Configuration
{
public Configuration()
{
this.ConfigurationElements = new HashSet<ConfigurationElement>();
this.AttachedMeters = new HashSet<AttachedMeter>();
}
public int GatewayId { get; set; }
public virtual Gateway Gateway { get; set; }
public DateTime Timestamp { get; set; }
public int? RefId { get; set; }
public int ConfigurationStatus { get; set; }
public int? ProfileId { get; set; }
public virtual Profile Profile { get; set; }
public virtual ICollection<ConfigurationElement> ConfigurationElements { get; set; }
public virtual ICollection<AttachedMeter> AttachedMeters { get; set; }
}
public class Gateway : IEquatable<Gateway>
{
public Gateway()
{
this.Scans = new HashSet<Scan>();
this.Configurations = new HashSet<Configuration>();
this.MonitoringDatas = new HashSet<MonitoringData>();
this.AdditionalInformations = new HashSet<AdditionalInformation>();
// to Delete for the UHF version 3.0
/* this.AttachedMeters = new HashSet<AttachedMeter>();*/
}
public int Id { get; set; }
public string Identifier { get; set; }
public Guid? TreeNodeId { get; set; }
public virtual TreeNode TreeNode { get; set; }
public int? ProfileId { get; set; }
public virtual Profile Profile { get; set; }
public virtual ICollection<Scan> Scans { get; set; }
public virtual ICollection<Configuration> Configurations { get; set; }
public virtual ICollection<MonitoringData> MonitoringDatas { get; set; }
public virtual ICollection<AdditionalInformation> AdditionalInformations { get; set; }
}
映射:
public class AttachedMeterMap : EntityTypeConfiguration<AttachedMeter>
{
public AttachedMeterMap()
{
this.HasKey(am => new { am.GatewayId, am.Timestamp, am.MeterId});
this.HasRequired(am => am.Configuration)
.WithMany(c => c.AttachedMeters)
.HasForeignKey(am => new {am.GatewayId, am.Timestamp});
this.HasRequired(am => am.Meter)
.WithMany(m => m.AttachedMeters)
.HasForeignKey(am => am.MeterId);
}
}
public ConfigurationMap()
{
this.HasKey(c => new { c.GatewayId, c.Timestamp });
this.Property(c => c.Timestamp).IsRequired();
this.Property(c => c.ConfigurationStatus).IsRequired();
this.HasRequired(c => c.Gateway)
.WithMany(g => g.Configurations)
.HasForeignKey(c => c.GatewayId);
this.HasOptional(c => c.Profile)
.WithMany(p => p.Configurations)
.HasForeignKey(c => c.ProfileId);
this.Property(c => c.RefId);
this.HasMany(c => c.AttachedMeters);
}
}
public GatewayMap()
{
this.HasKey(g => g.Id);
this.Property(g => g.Identifier).IsRequired().HasMaxLength(20);
this.HasOptional(g => g.TreeNode)
.WithMany()
.HasForeignKey(g => g.TreeNodeId);
this.HasOptional(g => g.Profile)
.WithMany()
.HasForeignKey(g => g.ProfileId);
this.HasMany(g => g.Scans);
this.HasMany(g => g.Configurations);
this.HasMany(g => g.MonitoringDatas);
}
在初始化databaseContext时,程序会抛出这样的错误: “关系约束中的从属角色和主要角色中的属性数必须相同”
当然,这是这些对象之间的关系,但我不知道如何...... 我的错误在哪里?