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: @RoMiller I don't understand why you can't patch EF5. From what I've seen, it's a one/ two line change your end. For me it's a nightmare!!! I have to individually go through each entity and ignore all the properties using model builder. Isn't that the entire point of using attributes in the first place. So you don't have to manually code using fluent. EF 5 is meant to be a release. Not a half finished release. Most people using SQL Server can ignore and go straight to EF 6 alpha. We on the other hand are not using SQL Server so are stuck with many lines of workaround code until EF 6 is released. Not helpful!!!!

Viewing all articles
Browse latest Browse all 10318

Trending Articles



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