automapper

AutoMapper How to map nested object from an ObjectId

荒凉一梦 提交于 2019-12-05 23:08:28
问题 I am trying to map the ReferralContract.AssessmentId property to Referral.Assessment.Id The below code works but I am sure that there is a cleaner way to do.... Please tell me this is so ;-) // Destination classes public class Referral { public Referral() { Assessment = new Assessment(); } public int Id { get; set; } public Assessment Assessment { get; set; } } public class Assessment { public int Id { get; set; } } // Source Class public class ReferralContract { public int Id { get; set; }

Should I use AutoMapper in my unit tests?

萝らか妹 提交于 2019-12-05 22:04:37
问题 I'm writing unit tests for ASP.NET MVC controller methods. Those controllers have a dependency on IMapper - an interface I've created to abstract AutoMapper, passed in via constructor injection using Castle Windsor. Action methods use IMapper to map from domain objects to ViewModel objects and back again, aiming to keep things DRY and keep action methods succinct. In my unit tests, should I Configure AutoMapper with the correct bindings (they're built using AutoMapper profiles, so testable

'entity of the same type already has the same primary key value' error using AutoMapper

痴心易碎 提交于 2019-12-05 22:01:33
When I use AutoMapper, this Error occured: Attaching an entity of type 'MyProject.DAL.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified'

AutoMapper - Why it is overwriting whole object? [duplicate]

余生长醉 提交于 2019-12-05 20:32:23
This question already has an answer here: Automapper: Update property values without creating a new object 3 answers I don't understand why it's overwriting my whole object. The reason is that I get my User object from db an I want to assign new values from DTO. Instead of just adding those new values it's creating new object that has new values but all previous are set to null . How can I make sure that in this case he will "upgrade" my object, not create new one? Scenario /users/{id} - PUT // User has id, username, fullname // UserPut has fullname public HttpResponseMessage Put(int id,

How to find the source property based on the name of a flattened property with AutoMapper

北战南征 提交于 2019-12-05 20:26:02
问题 I'm using AutoMapper and I'd like it to trace back a source property based on the name of the mapped (flattened) destination property. This is because my MVC controller has the name of a mapped property that it needs to provide to a service call that for sorting purposes. The service needs to know the name of the property that the mapping originated from (and the controller is not supposed to know it) in order to perform a proper call to the repository that actually sorts the data. For

AutoMapper Ignore on child collection property

橙三吉。 提交于 2019-12-05 20:02:30
I am trying to map object's of the same type which have a collection of child objects and am finding that Ignore() applied to properties on the child object seem to be umm... ignored! Here's a unit test which demonstrates the problem. class A { public int Id { get; set; } public string Name { get; set; } public ICollection<B> Children { get; set; } } class B { public int Id { get; set; } public string Name { get; set; } } [TestClass] public class UnitTest1 { [TestInitialize()] public void Initialize() { Mapper.CreateMap<A, A>() .ForMember(dest => dest.Id, opt => opt.Ignore()); Mapper.CreateMap

How to specify the shape of results with WebApi2, OData with $expand

[亡魂溺海] 提交于 2019-12-05 19:05:45
I have a problem executing my AutoMapper mappings when using OData with specific $select or $expand values. Using the WebApi Action: public IQueryable<BookInRequestDto> Get(ODataQueryOptions<BookInRequest> query) { var results = query.ApplyTo(_context.BookInRequests) as IQueryable<BookInRequest>; var mappedResults = Mapper.Map<IQueryable<BookInRequest>, IQueryable<BookInRequestDto>>(results); return mappedResults; } When I query: api/Get , I get an appropriate response, but a Document's Properties is set to null response containing documents properties set to null. When I query: api/Get?

Controlling Namespace Prefixes in WCF XML Output

孤街醉人 提交于 2019-12-05 18:37:24
The current output of my WCF service is as follows (only a portion is shown below): <s:Body> <executeSelectSP2Response xmlns="http://tempuri.org/"> <executeSelectSP2Result xmlns:a="http://schemas.datacontract.org/2004/07/WCF_Services.DataContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <a:Rows> <a:RowDetail> <a:Fields> <a:FieldDetail> <a:name>STATE_CD</a:name> <a:value>1</a:value> </a:FieldDetail> <a:FieldDetail> <a:name>STATE_CD_TXT</a:name> <a:value>Alabama</a:value> </a:FieldDetail> <a:FieldDetail> <a:name>STATE_CD_SHORT_TXT</a:name> <a:value>AL</a:value> </a:FieldDetail> </a

Injecting a specific instance of an interface using Autofac

浪尽此生 提交于 2019-12-05 18:34:44
I have a controller and it receives a specific instance of an interface. The interface looks something like this: public interface IMyInterface { ... implementation goes here } And then I have some classes that implement this interface like this: public class MyClassA : IMyInterface { ... implementation goes here } public class MyClassB : IMyInterface { ... implementation goes here } In my ControllerA I have the following constructor: private ICustomerService customerService; private IMyInterface myInterface; puvlic ControllerA(ICustomerService customerService, IMyInterface myInterface) { this

Mapping Domain Models to View Models

烈酒焚心 提交于 2019-12-05 18:29:36
I'm starting from a point very similar to: Domain Entities, DTO, and View Models . The advised use of DTOs to map between the domain model and the MVC's ViewModel seems consistent expectations. I seek details of how to bridge the domain model (Entity Framework-based project) to the WebAPI mvc project. I'm starting with a project of simple POCOs (generated by EF PowerTools to reverse engineer my existent db) that I want to connect to an MVC4 WebAPI project. I expect I'll be adding business logic to the baseline POCO project as my solution evolves and perhaps this is the crux of this issue. The