0

EF 4.0 コードのみを使用して、抽象クラスと通常クラスを関連付けたいと考えています。

クラス「Item」、「ContentBase」、および「Test」があります。

「ContentBase」は抽象的で、「Test」はそこから派生しています。

「ContentBase」には、「Item」のインスタンスにリンクするプロパティ「Item」があります。

そのため、「Test.Item」または「ContentBase」から派生したクラスには「Item」ナビゲーション プロパティがあります。

私のDBでは、TestのすべてのレコードにItemの一致するレコードがあります。


public class Item
{
    public int Id { get; set;}
}

public abstract class ContentBase
{
    public int ContentId { get; set;}
    public int Id { get; set;}

    public Item Item { get; set;}
}

public class Test : ContentBase
{
    public string Name { get; set;}
}

今いくつかの初期化コード

public void SomeInitFunction()
{

    var itemConfig = new EntityConfiguration<Item>();
    itemConfig.HasKey(p => p.Id);
    itemConfig.Property(p => p.Id).IsIdentity();
    this.ContextBuilder.Configurations.Add(itemConfig);

    var testConfig = new EntityConfiguration<Test>();
    testConfig.HasKey(p => p.ContentId);
    testConfig.Property(p => p.ContentId).IsIdentity();

    // the problem 
    testConfig.Relationship(p => p.Item).HasConstraint((p, q) => p.Id == q.Id);

    this.ContextBuilder.Configurations.Add(testConfig);  
}

これによりエラーが発生します: 派生型 'Test' のキーが登録されています。ルート タイプ「ContentBase」のキーを登録する必要があります。

とにかく私はエラーが発生しようとします。私は何を間違っていますか?

4

3 に答える 3

0

ここまでの結論:ありえない。

于 2010-07-23T16:43:41.270 に答える
0

suppertype エンティティで試して、関係を再宣言します。

ContentBase: public virtual Item Item { get; 設定;}

テスト中: public new Item Item { get; 設定;}

これは私のテストプロジェクトでコンパイルされましたが、それ以上テストしませんでした。とりあえずwheb EF5かファイナルカメオか

于 2010-11-06T13:33:24.823 に答える
0

ナビゲーション プロパティ Item はベース タイプ レベルで定義されるため、モデルはベース タイプを認識します。Item プロパティを渡すテスト クラスにプロパティを追加し、それをリレーションにマップできます。

public class Test : ContentBase
{
    public string Name { get; set;}
    public string TestItem { get {return Item;} ...

}

testConfig.Relationship(p => p.TestItem )
于 2010-05-16T13:27:53.397 に答える