2

アプリを接続する必要がある従来のテーブルがあります。コードファーストの POCO モデルを使用しています。私は次のクラスを持っています:

public class Equipment
{
    [Key]
    public string EquipmentId { get; set; }
    public string OriginatorId { get; set; }

    public virtual Employee Employee { get; set; }
}

public class Employee
{
    [Key]
    [Column("employee_id")]
    public string EmployeeId { get; set; }

    public string EmployeeName { get; set; }

    [ForeignKey("OriginatorEmployeeId")]
    public virtual Equipment Equipment { get; set; }
}

Employee クラスの EmployeeId を Equipment クラスの OriginatorEmployeeId にマップする必要があります。

また、従来のテーブルは Employee クラスによって表されます。テーブルの名前は実際には employee (小文字) で、EmployeeId 列の名前は「employee_id」です。クラスとプロパティの名前をアプリの残りの部分と一致させたいので、Employee と EmployeeId.

流暢なAPIを使用して試したことは次のとおりです。

    modelBuilder.Entity<Employee>().Map(m =>
    {
        m.MapInheritedProperties();
        m.ToTable("employee");
    });

    modelBuilder.Entity<Equipment>()
                .HasOptional<Employee>(u => u.Employee)
                .WithOptionalDependent(c => c.Equipment).Map(p => p.MapKey("OriginatorEmployeeId"));

私はおそらく必要のないものを混ぜています。私が今得ているエラーは次のとおりです。

Multiplicity is not valid in Role 'Equipment_Employee_Source' in relationship 'Equipment_Employee'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

どんな助けでも大歓迎です。

4

1 に答える 1

4

従業員レコードを複数の設備レコードに関連付けることはできますか? 可能であれば、Employee POCO には、Employee と Equipment の間の 1 対多の関係を表すコレクション プロパティが含まれている必要があります。

public virtual ICollection<Equipment> Equipments {get;set;}

次に、この関係を示すように構成を調整する必要があります。

modelBuilder.Entity<Employee>()
            .HasMany<Equipment>(u => u.Equipments)
            .WithRequired(c => c.Employee).HasForeignKey(p => p.OriginatorId);

また、列名マッピングの構成もセットアップする必要があるようです。したがって、POCO ごとに個別の構成ファイルを作成して構成を管理しやすくし、それらの構成を DBContext の OnModelCreating イベントの modelbuilder.Configurations コレクションに追加することをお勧めします。

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelbuilder.Configurations.Add(new EmployeeConfiguration());
}
于 2013-08-06T15:46:39.560 に答える