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.