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

Edited 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 ##

Here's the algorithm to fix your code if you get the exception "You cannot use Ignore method on the property 'PropertyX' on type 'TypeY' because this type inherits from the type 'TypeZ' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type.":

1. Are you calling modelBuilder.Entity<TypeY>().Ignore(p => p.PropertyX);?
Yes. Replace this call with modelBuilder.Entity<TypeZ>().Ignore(p => p.PropertyX);
Goto 3.
No. Goto 2.
2. There should be a NotMapped attribute on property 'PropertyX' that's defined on 'TypeZ'. Is 'TypeZ' included in the model? (i.e. it's a valid entity type and it isn't ignored)
Yes. Ensure that 'TypeZ' is discovered before 'TypeY', to do this put modelBuilder.Entity<TypeZ>(); before modelBuilder.Entity<TypeY>();
Goto 3.
No. Remove NotMapped attribute from property 'PropertyX'. For each 'TypeW' directly derived from 'Z' add modelBuilder.Entity<TypeW>().Ignore(p => p.PropertyX);
Goto 3.
3. Run your tests. Did you get another exception with the same text, but different types?
Yes. Goto 1.
No. Enjoy!



Viewing all articles
Browse latest Browse all 10318

Trending Articles



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