7

最初にEFPOCOプロジェクトをコードに変換しています。T4テンプレートを変更して、すべてのエンティティが基本クラスを使用するようにEntityBaseしました。これにより、永続性に関連しないいくつかの一般的な機能が提供されます。

[NotMapped]で属性を使用するとEntityBase、すべてのエンティティがこの属性を継承しThe type 'X.X.Person' was not mapped、EFで使用しようとするすべてのタイプに対してを取得します。

[NotMapped]のすべてのプロパティで使用するEntityBaseと、EntityType 'EntityBase' has no key defined. Define the key for this EntityType例外が発生します

参考:私はEf4.3.1を使用しています

編集:コードの一部:

[DataContract(IsReference = true)]
public abstract class EntityBase : INotifyPropertyChanged, INotifyPropertyChanging
{
    [NotMapped]
    public virtual int? Key
    {
        get { return GetKeyExtractor(ConversionHelper.GetEntityType(this))(this); }
    }
    //other properties and methods!
}

その後

[DataContract(IsReference = true), Table("Person", Schema = "PER")]
public abstract partial class Person : BaseClasses.EntityBase, ISelfInitializer
{
    #region Primitive Properties
    private int? _personID;
    [DataMember,Key]
    public virtual int? PersonID
    {
        get{ return _personID; }
        set{ SetPropertyValue<int?>(ref _personID, value, "PersonID"); }
    }
}

これらの2つのクラスには、流暢なAPI構成はありません。

4

2 に答える 2

4

可能であればEntityBaseas として定義し、マップしたくないプロパティに ... を配置してみてください。abstractNotMapped

于 2012-08-27T12:12:06.387 に答える
4

すべてのエンティティが共有するEntityBase のテーブルを作成しようとしていますか (これについてのすばらしいブログ投稿: Type Inheritence ごとのテーブル)、または単にベース オブジェクトを作成して、すべてのエンティティが同じメソッドを使用できるようにしようとしていますか? 上記のコードで問題はありませんでした。クイック テスト アプリの全体を次に示します。

[DataContract(IsReference = true)]
public abstract class EntityBase
{
    [NotMapped]
    public virtual int? Key
    {
        get { return 1; } //GetKeyExtractor(ConversionHelper.GetEntityType(this))(this); }
    }
    //  other properties and methods!
}

[DataContract(IsReference = true)]
public partial class Person : EntityBase
{
    private int? _personID;
    [DataMember, Key]
    public virtual int? PersonID
    {
        get { return _personID; }
        set { _personID = value; }
    }
}

public class CFContext : DbContext
{
    public DbSet<Person> Employers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

    }
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person();
        Console.WriteLine(p.Key);
    }
}

このテーブル/データベースを作成したのはどれですか: ここに画像の説明を入力

于 2012-08-27T14:27:32.507 に答える