简体   繁体   中英

Entity Framework, how to work with not mapped entities

I've got a doubt about working with not mapped POCO entities. I have an Entity that contains properties(and this properties are other POCO classes that are mapped to the database).

I've got a generic repository where this repository receives an generic entity to work with:

public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
{
    internal readonly DbContext recuperadorContext;
    internal readonly IDbSet<TEntity> dbSet;

    public BaseRepository(DbContext context)
    {
        recuperadorContext = context;
        dbSet = recuperadorContext.Set<TEntity>();
    }

    public TEntity Adicionar(TEntity obj)
    {
        var objReturn = dbSet.Add(obj);
        return objReturn;
    }

}

The implementation is:

public class CupomFiscalRepository : BaseRepository<CupomFiscalDetalhes> 
{
}

The not mapped entity with it's properties is described bellow:

public class CupomFiscalDetalhes
{
    public Movimento Movimento { get; set; } //POCO class mapped
    public Cliente ClienteCRM { get; set; } //POCO class mapped
    public Vendedor Vendedor { get; set; } //POCO class mapped
}

Since CupomFiscalDetalhes does not have a table to be mapped, do I need to have all the POCO classes mapped in EntityFramework to work with ht? Asking because I don't know how I'm going to save these data to the database with the generic repository.

If you need to persist CupomFiscalDetalhes then you should add it to the context. If you don't need to persist CupomFiscalDetalhes then it shouldn't implement IBaseRepository and you should consider it a view of data populated via a controller class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM