Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Issue: NotMapped on base property throws if derived type discovered first [481]

$
0
0
An exception is thrown when the derived entity type is discovered before the base entity type containing an ignored property.

"You cannot use Ignore method on the property 'BaseProperty' on type 'DerivedEntity' because this type inherits from the type 'BaseEntity' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type."

var modelBuilder = new DbModelBuilder();
modelBuilder.Entity<DerivedEntity>();
modelBuilder.Entity<BaseEntity>();
modelBuilder.Build(new DbProviderInfo("System.Data.SqlClient", "2008"));

public class BaseEntity
{
public long Id { get; set; }
[NotMapped]
public string BaseProperty { get; set; }
}

public class DerivedEntity : BaseEntity
{
}

## WORKAROUND ##

The workaround would be to make sure that the base entity type is discovered first. Usually this means adding an entity set for it to the context or removing the entity set for the derived type. In this repro however it's enough to just swap the .Entity() calls.
Alternatively the base entity type could be explicitly ignored.

Comments: @nalvest: In your case the workaround is to use Ignore on every derived entity. ``` modelBuilder.Entity<DerivedEntity>().Ignore(e => e.BaseProperty); ```

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>