1

オブジェクトのリストを含むクラスを含む.dllがあります(これはメタデータからのもので、ソースコードはありません):

public class APIKeyInfo
{
    public APIKeyInfo();
    public long AccessMask { get; set; }
    public List<AccountEntry> Characters { get; set; }
    public DateTime Expires { get; set; }
}

public class AccountEntry
{
    public AccountEntry();
    public long CharacterID { get; set; }
    public string Name { get; set; }
    ... other properties
}

私のコードには、EF Code First を使用してデータベース テーブルを指定する APIKeyInfo の拡張クラスがあります。

[Table("MyApiKey")]
public class MyApiKey : APIKeyInfo
{
    [Key]
    public int KeyId { get; set; }
    [Required]
    public string vCode { get; set; } 
}

問題は、これにより EF が APIKeyInfo 内の「リスト文字」プロパティのテーブルを作成しようとすると (MyApiKey テーブルへの FK があると仮定します)、キーが持っているというエラーが表示されることです。定義されていません。ソース コードにアクセスできない場合、"List Characters" プロパティのキーを指定するにはどうすればよいですか? 私は次のようなことを望みます:

[Table("MyApiKey")]
public class MyApiKey : APIKeyInfo
{
    [Key]
    public int KeyId { get; set; }
    [Required]
    public string vCode { get; set; } 

    [Key]
    base.Characters.CharacterID;
}

どのプロパティを使用するかを伝えるだけですが、明らかにそれは機能しません。

4

1 に答える 1

1

私は実際にこれを試したことはありませんが、なぜうまくいかないのかわかりません。属性を使用する代わりに、EntityTypeConfiguration オブジェクトを使用してみてください。

 public class MyApiKeyMapping : EntityTypeConfiguration<MyApiKey>
 {
     public MyApiKeyMapping()
     {
         this.ToTable("MyApiKey");
         this.HasKey(k => k.KeyId);
         this.Property(p => p.vCode).IsRequired();
     }
}

public class AccountEntryMapping : EntityTypeConfiguration<AccountEntry>
{
    public AccountEntryMapping()
    {
        this.ToTable("AccountEntry");
        this.HasKey(k => k.CharacterId);
    }
}

これにより、所有していないエンティティのマッピングを作成できるようになります (うまくいけば)。

次に、コンテキストに次を追加するだけです。

public class MyApiContext : DbContext
{

    ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new MyApiKeyMapping());
        modelBuilder.Configurations.Add(new AccountEntryMapping());
    }
}

マッピングに設定する必要がある関係やその他のプロパティを追加することもできます。

于 2013-06-20T19:24:54.653 に答える