automapper

Many-to-many to DTOs using Automapper

匆匆过客 提交于 2019-12-03 16:37:27
If I have a many-to-many relationship defined in EF: public class StudentImage { public int StudentId { get; set; } public int ImageId { get; set; } public int Order { get; set; } public virtual Student Student { get; set; } public virtual Image Image { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<StudentImage> Images { get; set; } } public class Image { public int Id { get; set; } public string Filename { get; set; } public virtual ICollection<StudentImage> Students { get; set; } } And the DTO's: public class

How do I use AutoMapper with Ninject.Web.Mvc?

浪子不回头ぞ 提交于 2019-12-03 16:34:18
问题 Setup I have an AutoMapperConfiguration static class that sets up the AutoMapper mappings: static class AutoMapperConfiguration() { internal static void SetupMappings() { Mapper.CreateMap<long, Category>.ConvertUsing<IdToEntityConverter<Category>>(); } } where IdToEntityConverter<T> is a custom ITypeConverter that looks like this: class IdToEntityConverter<T> : ITypeConverter<long, T> where T : Entity { private readonly IRepository _repo; public IdToEntityConverter(IRepository repo) { _repo =

Automapper Convention

我们两清 提交于 2019-12-03 15:17:58
Is is possible with Automapper to setup a convention so that maps do not have to be created by hand for situations where the entity you are mapping to just has say "ViewModel" appended. As an example I would rather not have to setup the following map: Mapper.CreateMap<Error, ErrorViewModel>(); I understand if projection is required that I would need to create a custom map, but having a convention to create maps would be nice. You would need to use Mapper.DynamicMap<TDest>(source) to map. As you can see in the example below, it automatically maps the matching properties from source to

AutoMapper and convert a datetime to string

我的梦境 提交于 2019-12-03 15:06:18
问题 I can't get my head round the following issue. I have a feeling it is a limitation of LINQ and expression trees, but not sure how to accept the lambda body. Can I achieve this WITHOUT creating a custom converter? Mapper.CreateMap<I_NEWS, NewsModel>() .ForMember(x => x.DateCreated, opt => opt.MapFrom(src => { var dt = (DateTime)src.DateCreated; return dt.ToShortDateString(); })); I'm getting this error: A lambda expression with a statement body cannot be converted to an expression tree 回答1:

AutoMapper - How to pass parameters into a Custom Resolver using ConstructedBy method?

十年热恋 提交于 2019-12-03 14:49:35
In my ASP.NET MVC 2 (RC) project - I'm using AutoMapper to map between a Linq to Sql class (Media) and a view model (MediaVM). The view model has a SelectList property for a drop down in the view. I have a custom value resolver to populate the SelectList property items from the db, but am wondering if there's a way to pass a couple values from the source model into the resolver (using ConstructedBy method?) to a) define the selected item and b) filter the items from the db. The source object gets passed into the custom resolver - but the resolver is used on several different view models with

.net Core 使用AutoMapper

怎甘沉沦 提交于 2019-12-03 14:48:57
在我们的项目中慢慢的要把数据库的实体模型和视图模型进行分离,防止被人拿到我们表字段。在学校的时候自己只是有将很多数据库模型,写成一个视图模型返回到前台。 首先我们把这两个包引入项目中去。 然后我们创建一个转换配置类,这个类要继承 Profile 将我们需要转换的类写到我们构造函数里面去,这里要注意我们左边的UserEntity是要将这个类型的数据转换成UserModel 当然这个可以反过来,但是你转换的关系一定要正确,左为要转换的数据类型,右边是转换后的数据类型。(我已经帮你们测试了,关系不对乱转报错。) 下面我们创建两个类 这个是我们平常中数据库表实体 这是我们的视图模型 这个里要注意,在转化的时候只有名字相同的字段才会成功附上对应的值。(大小写可以不管,但是在项目中要做到一致,我上面没有一致是测试一下。) 然后就是使用了 我们只要在使用的地方,注入进来了,就可以使用了。 这个是单个实体的转换 简写方法,直接写要转成什么类型就可以了,但是转换的配置类法不变。 当然我们还有集合的转换了 好了使用就是这样,我之前也使用反射做过类似的功能。后面我整理一个,写一个使用反射写一个自己的。 来源: https://www.cnblogs.com/chenxi001/p/11800943.html

Do i need to create automapper createmap both ways?

匆匆过客 提交于 2019-12-03 14:25:39
问题 This might be a stupid question! (n00b to AutoMapper and time-short!) I want to use AutoMapper to map from EF4 entities to ViewModel classes. 1) If I call CreateMap<ModelClass, ViewModelClass>() then do I also need to call CreateMap<ViewModelClass, ModelClass>() to perform the reverse? 2) If two classes have the same property names, then do I need a CreateMap statement at all, or is this just for "specific/custom" mappings? 回答1: In AutoMapper you have a Source type and a Destination type. So

Automapper + EF4 + ASP.NET MVC - getting 'context disposed' error (I know why, but how to fix it?)

扶醉桌前 提交于 2019-12-03 14:12:24
I have this really basic code in a MVC controller action. It maps an Operation model class to a very basic OperationVM view-model class . public class OperationVM: Operation { public CategoryVM CategoryVM { get; set; } } I need to load the complete list of categories in order to create a CategoryVM instance. Here's how I (try to) create a List<OperationVM> to show in the view. public class OperationsController : Controller { private SomeContext context = new SomeContext (); public ViewResult Index() { var ops = context.Operations.Include("blah...").ToList(); Mapper.CreateMap<Operation,

Automapper and immutability

自闭症网瘾萝莉.ら 提交于 2019-12-03 13:03:33
Is it possible to use AutoMapper with Immutable types? For example my Domain type is immutable and I want to map my view type to this. I believe it is not but just want this confirmed. Also as it is best practice to have your domain types immutable, what is the best practice when mapping your view types to domain types? I typically do the mapping from view types to domain types by hand, as I'll typically be working through a more complex interface, using methods and so on. If you use AutoMapper to go from view to domain, you're now locked in to an anemic domain model, whether you've

Force throwing of exception when a source property is unmapped

情到浓时终转凉″ 提交于 2019-12-03 12:59:37
In AutoMapper 2.2.1, is there any way I can configure my mappings so that when a property is not explicitly ignored, an exception is thrown? For example, I have the following classes and configuration: public class Source { public int X { get; set; } public int Y { get; set; } public int Z { get; set; } } public class Destination { public int X { get; set; } public int Y { get; set; } } // Config Mapper.CreateMap<Source, Destination>(); The behavior I receive with this configuration is that the Destination.X and Destination.Y properties are set. Furthermore, if I test my configuration: Mapper