Entity Framework and Automapper Data Prediction

I want to use AutoMapper to create a ViewModel (flattening - data projection) for use in an ASP.net MVC application.

var tmp = from x in db.Mailings select Mapper.Map<Mailing, MailingViewModel>(x);
return View(tmp.ToList());

Of course, when I try the example above, I get an EF error. "LINQ to Entities does not recognize the method ... method, and this method cannot be translated into a repository expression."

I know that you can move .ToList () before Automapper does its magic, but then I retrieve all the fields from Db (and I only need 3 out of 20 fields)

Is it possible to use this in a clean way. Clear = not all fields are retrieved from the database, but only the fields necessary for the ViewModel. Is this possible in Automapper? Or maybe in another library? (without doing it manually;))

+5
source share
6 answers

Yes, it is very possible. See here http://www.devtrends.co.uk/blog/stop-using-automapper-in-your-data-access-code

Edit: I recently discovered that a framework for this already exists in AutoMapper. add a using statement for AutoMapper.QueryableExtensions and you are provided with an IQueryable extension called Project <> ()

+9
source

You can simply call:

var tmp = from x in db.Mailings 
          select new MailingViewModel
            {
               FirstName = x.FirstName,
               LastName = x.LastName,
               Address = x.Address
            };

You do not need AutoMapper for a simple projection if you access the EF directly in the controller.

AutoMapper linq-to-entities - . ( ), AutoMapper AutoMapper.

+4

, linq IQueryableProviders ( , ).

, , Linq , linq sql. linq , Mapper.Map<> SQL, , .

, linq: http://channel9.msdn.com/Shows/Going+Deep/Erik-Meijer-and-Bart-De-Smet-LINQ-to-Anything

+2

, AutoMapper DynamicMap. , - , AutoMapper, Ladislav Mrnka.

var tmp = from x in db.Mailings 
          select new
          {
              FirstName = x.FirstName,
              LastName = x.LastName,
              Address = x.Address
          };

return View(tmp.ToList().Select(item => Mapper.Map<MailingViewModel>(item)));

, , , , , AutoMapper. AutoMapper, .

+2

Automapper ( ), :

:

  model.Sales = _dbcontext.Sales.Where(o => o.PartnerId == PartnerId && (o.SaleDate > model.BeginDate || model.BeginDate == null) && (o.SaleDate <= model.EndDate || model.EndDate == null)).Select(o => new SaleViewModel
        {
            NumberIn1S = o.NumberIn1S,
            Total = o.Total,
            SaleDate = o.SaleDate,
            Comments = o.Comments,
            Driver = o.Driver,
            GuidIn1S = o.GuidIn1S
        }).OrderByDescending(o => o.SaleDate).ToList(); </p>

MyMapper:

model.Sales = _dbcontext.Sales.Where(o => o.PartnerId == PartnerId && (o.SaleDate > model.BeginDate || model.BeginDate == null) && (o.SaleDate <= model.EndDate || model.EndDate == null)).OrderByDescending(o => o.SaleDate).ToArray().Select(p => <b>MyMapper<SaleViewModel>.CopyObjProperties(p, "NumberIn1S,Total,SaleDate,Comments,Driver,GuidIn1S")</b>).ToList(); </p>
+1

All Articles