をabstract class BaseEntity
に無視する必要がありますが、[NotMappedAttribute]
を使用せずにこの機能を使用することはできませんが、属性を使用すると無視されますOData APIで使用します。Entity Framework 6:すべての派生型のbasetypeプロパティを無視する
私はここでこれをテストするためにgithubのプロジェクトを設定している: https://github.com/nurf3d/EntityFrameworkDerivedTypesIgnoreProperiesTest/tree/master
継承チェーン:
[NotMappedAttribute]
を使用して、State
properyがために無視されます:
public abstract class BaseEntity
{
[Key]
public int ID { get; set; }
[Timestamp]
public byte[] Rowversion { get; set; }
public State State { get; set; }
}
[Table("Events")]
public abstract class Event : BaseEntity
{
public int PersonID { get; set; }
public string EventName { get; set; }
public virtual Person Person { get; set; }
}
public class DerivedEvent1 : Event
{
public bool IsDerivedEvent1 { get; set; }
}
public class DerivedEvent2 : Event
{
public bool IsDerivedEvent2 { get; set; }
}
が属性すべての型が正しく、移行は正常に実行されますが、これにより不要なOData APIからプロパティも削除されます。
OData API内でState
プロパティが必要なため、[NotMappedAttribute]
ではなく、流暢な設定を使用しています。
流暢な構成:このエラーで
modelBuilder.Types<BaseEntity>().Configure(clazz => clazz.Ignore(prop => prop.State));
add-migration Initial -Force
結果:
You cannot use Ignore method on the property 'State' on type 'EntityFrameworkIgnoreProperty.Models.DerivedEvent1' because this type inherits from the type 'EntityFrameworkIgnoreProperty.Models.BaseEntity' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type.
私はこれが流暢APIで動作するように取得する必要があると私はでBaseEntity
のすべての派生型のためにこれを実行する必要があります一度。
私の実際のプロジェクトでは、100以上のエンティティがあります。これは、個々のエンティティごとに手作業で行うことはできません。
は(すなわち、EFの継承に参加)のエンティティとしてマッピングされた 'BaseEntity'クラスですか? –
'BaseEntity'は、別個のエンティティとしてマッピングされていません。この例では、イベントのみをTPHを持つエンティティとしてマップする必要があります。 – Nurfed