Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 6.1 Fluent API: Ignore property of the base class

I have a base class for all entities:

public class BaseClass
{
    public int SomeProperty {get; set;}
}

public class SomeEntity : BaseClass
{
    ...
}

I want to ignore this property in some cases. Could I do in the OnModelCreating method something like this:

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties<int>()
                    .Where(p => p.Name == "SomeProperty")
                    .Ignore();
}

?

like image 505
igoodwin Avatar asked Oct 16 '25 12:10

igoodwin


1 Answers

You could try:

modelBuilder.Entity<SomeEntity>().Ignore(p => p.SomeProperty);

It will cause SomeProperty not to be mapped to SomeEntity.

EDIT: If this property should never be mapped to database you can add NotMapped annotation in your BaseClass:

public class BaseClass
{
    [NotMapped]
    public int SomeProperty {get; set;}
}

This will be the same as ignoring this property in all extending classes.

like image 137
Michał Krzemiński Avatar answered Oct 19 '25 02:10

Michał Krzemiński