1

I use the same entity in two DbContexts.

public class ActualDbContext : DbContext
{
    public DbSet<Doc> Docs { get; set; }
}

public class ArchivedDbContext : DbContext
{
    public DbSet<Doc> Docs { get; set; }
}

public class Doc
{
    ...
}

How can I add an ignored field to this entity, but fill it from DbContext ?

public class Doc
{
    ...
    public bool IsArchived {get;set;}
}

public class ActualDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder builder)
    {

        builder.Entity<Doc>(entity =>
        {
            entity
                .Ignore(x => x.IsArchived)
                .WithValue(x => false);        // ??
        };
    }
}


public class ArchivedDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Doc>(entity =>
        {
            entity
                .Ignore(x => x.IsArchived)
                .WithValue(x => true);        // ??
        };
    }
}

When reading data from a database, this property must be filled in. In the program code, I want to avoid duplicating the code. I need to work with these entities in the same way. With the exception of this property. How can I do this ?

@ali you mean something like that ?

class MyRepo: IRepo
{
    private bool _isArchived;

    public MyRepo (MyContext context, bool isArchived)
    {
        _context = context;
       _isArchived = isArchived;
    }

    public IQueryable<Doc> GetData (...)
    {
        return _mainDbContext.Docs.Where(....)
            .Select (x=> new Doc 
            {
                Id = x.Id,
                IsArchived = _isArchived 
            });
    }

}

I don't really like this decision.

Michael Zagorski
  • 107
  • 1
  • 10

2 Answers2

0

Based on ef core architecture, it's impossible to do such a thing. I suggest to use the repository pattern and set the default value for your entities when fetching the data.

Class MyRepo: IRepo
{
    private ActualDbContext _mainDbContext;
    private ArchivedDbContext _archivedContext;
    public MyRepo (ActualDbContext context , ArchivedDbContext archivedContext)
    {
        _mainDbContext = context;
        _archivedContext = archivedContext;
    }
    public IQueryable<Doc> GetMainData (...)
    {
        return _mainDbContext.Docs.Where(....)
            .Select (x=> new Doc 
            {
                Id = x.Id,
                IsArchived = false
            });
    }

    public IQueryable<Doc> GetArchivedData (...)
    {
        return _archivedContext.Docs.Where(....)
            .Select (x=> new Doc 
            {
                Id = x.Id,
                IsArchived = true
            });
    }

}
Ali Kamali
  • 103
  • 1
  • 1
  • 8
0

So, you can do this by overriding onmodelcreating to its base class.

public class ActualDbContext : DbContext
{
    public ActualDbContext (DbContextOptions<ActualDbContext > options) : base(options)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Doc>().Ignore(t => t.IsArchived);
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<Doc> Docs { get; set; }
}

Check this answer for more explanation => ignoring-a-class-property-in-entity

Good Day

Pritom
  • 57
  • 1
  • 12
  • My question is about something else. Not about how to configure the ignored field. It's about how to fill it in within the context. – Michael Zagorski Mar 30 '20 at 11:29
  • I am confused about your question. why you want to ignore a property if you want to fill your desire value in it ? If you ignore it, does it matter what you fill with. It will not count when you fetch your value from db. right ? – Pritom Mar 30 '20 at 12:29
  • In the program code, I process archived and current documents identically, except for a couple of lines of difference. I need to show that the record is archived or actual. – Michael Zagorski Mar 30 '20 at 13:08
  • Okay, So you want to show all of your document IsArchived status is true ? Help me to understand what do you want to show and what is your current db status. – Pritom Mar 30 '20 at 13:41