AutoMapper not updating a nullable long? to null - c#

I have the following two classes:
public class DomainStudent {
public long Id { get; set; }
public string AdvisorId { get; set; }
public long? DegreeId { get; set; }
}
public class ApiStudent {
public long Id { get; set; }
public long AdvisorName { get; set; }
public long? DegreeId { get; set; }
}
When I run the following mapping:
Mapper.CreateMap<ApiStudent, DomainStudent>();
var api = new ApiStudent();
api.Id = 123;
api.AdvisorName = "Homer Simpson";
api.DegreeId = null;
var domain = new DomainStudent();
domain.Id = 123;
domain.AdvisorName = "Marge Simpson";
domain.DegreeId = 5; // i want this to get written to null
Mapper.Map(api, domain);
// at this point domain.DegreeId = 5 instead of null
I would have thought this worked by default. Am I missing something?

By default automapper will ignore null source values.
You can change this with the following:
Mapper.Initialize( Conf =>
{
Conf.ForSourceType<ApiStudent>().AllowNullDestinationValues = true;
} );
Otherwise you can try:
Mapper.CreateMap<long?, long?>()
.ConvertUsing(v => v);
Pretty ugly hack to have to do something like this but it might work.
edit: Just for clarity I wanted to note the final solution to the question was upgrading to AutoMapper version 3.2.1

Related

Entity Framework Include/select only certain properties from different tables

Let's say I have a method called GetThreadWithComments(). Each thread has 1 user (the creator) and has a list of comments. Each comments has 1 user (the poster).
Here are the classes (generated by EF):
public class Thread
{
public int ThreadId { get; set; }
public int UserId { get; set; }
public string Message { get; set; }
public List<Comment> Comments { get; set; }
public User User { get; set; }
}
public class Comment
{
public long CommentId { get; set; }
public string Message { get; set; }
public int UserId { get; set; }
public int ThreadId { get; set; }
public User User { get; set; }
}
So basically, I want to load a thread with user info, and associated comments with user info. I've tried something like this:
db.Threads.Select(x => new
{
x,
x.User = new { x.User.Username, x.User.Email },
x.Comments = x.Comments.Select(c => new
{
c.Message,
c.CommentId,
c.User = new { c.User.Username, c.User.Email }
})
});
The above does not work. However, I am not too sure on how to correctly do this. I could use include, but that would generate all properties. Since speed is a concern, I am trying to keep things as light as possible.
Reason it does not work: it does not build. Compile time error. The 2 errors I get are:
Cannot implicitly convert type '' to...
and
CS0746 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
First, define entity relationships as virtual, for example
public User User { get; set; }
should be
public virtual User User { get; set; }
Second, in case of the later posted compiler error, try adding the member names.
So instead of
x.User = new { x.User.Username, x.User.Email }
use
x.User = new { Username = x.User.Username, Email = x.User.Email }
Also there is too much x in there. The corrected example would be:
db.Threads.Select(x => new
{
x,
User = new { Username = x.User.Username, Email = x.User.Email },
Comments = x.Comments.Select(c => new
{
c.Message,
c.CommentId,
User = new { Username = c.User.Username, Email = c.User.Email }
})
});
Try this,
var result = db.Threads.Include(thread => thread.Comments);
Hope helps,

Invalid initializer member declarator in unit test mock up

I have a class
public class Restrictions
{
[Key]
public short RestrictionId { get; set; }
public string Description { get; set; }
public string BlockCode { get; set; }
public List<CustomerRestrictions> CustomerRestrictions { get; set; }
}
And
public class CustomerRestrictions
{
public int CustomerId { get; set; }
public CustomerContacts CustomerContacts { get; set; }
public string RestrictionId { get; set; }
public Restrictions Restrictions { get; set; }
}
Then
public class CustomerContacts
{
[Key]
public long CustomerId { get; set; }
public string Btn { get; set; }
public List<CustomerRestrictions> CustomerRestrictions { get; set; }
}
It seems to be many-to-many relationship.
Now I want to do a unit test for the controller. There is a similar example.
But it doesn't have many to many.
My controller is
[Route("api/[controller]")]
public class BtnRulesController : Controller
{
private readonly IBtnRulesRepository _btnRulesRepository;
public BtnRulesController(IBtnRulesRepository btnRulesRepository)
{
_btnRulesRepository = btnRulesRepository;
}
// GET api/BtnRules
[HttpGet]
public IList<Restrictions> Get()
{
return _btnRulesRepository.GetRestrictions();
}
The difficult thing is how to create sample data in unit test at this moment.
public class SimpleBtnRulesControllerTest
{
[Fact]
public void GetAllBtnRules_Should_Return_All_BtnRules()
{
var repo = new Mock<IBtnRulesRepository>().Object;
var controller = new BtnRulesController(repo);
var testBtnRules = GetTestBtnRules();
var result = controller.Get();
Assert.Equal(testBtnRules.Count,result.Count);
}
public List<Restrictions> GetTestBtnRules()
{
var testBtnRules = new List<Restrictions>();
var testCustomerRestrictionsList = new List<CustomerRestrictions>();
var testCustomerRestrictions = new CustomerRestrictions();
testCustomerRestrictions.CustomerId = 1;
testCustomerRestrictions.RestrictionId = "1";
testCustomerRestrictions.Restrictions=new Restrictions();
testCustomerRestrictions.CustomerContacts=new CustomerContacts();
testCustomerRestrictionsList.Add(new CustomerRestrictions());
testBtnRules.Add(new Restrictions() {RestrictionId = 1, Description = "Demo1",BlockCode = "AdminBlock1",testCustomerRestrictionsList});
testBtnRules.Add(new Restrictions() { RestrictionId = 2, Description = "Demo2", BlockCode = "AdminBlock2" ,testCustomerRestrictionsList});
return testBtnRules;
}
However I get the error CS0747 Invalid initializer member declarator.
There are two things incorrect with your code that I can see.
Firstly, when you are setting the testCustomerRestrictionsList you do not name the Property that it is being assigned to and this is the compiler error.
Your code should look like:
testBtnRules.Add(new Restrictions() {RestrictionId = 1, Description = "Demo1",BlockCode = "AdminBlock1", CustomerRestrictions = testCustomerRestrictionsList});
In this case I wouldn't worry too much about hardcoding the Restrictions into your unit test. But in the future you may want to look into something like AutoFixture so that you don't need to worry so much about creating your objects.
Secondly, you are not setting up the call to the mock IBtnRulesRepository when the GetRestrictions method is called.
Your test should look like:
[Fact]
public void GetAllBtnRules_Should_Return_All_BtnRules()
{
var repo = new Mock<IBtnRulesRepository>();
var testBtnRules = GetTestBtnRules();
repo.Setup(mock => mock.GetRestrictions())
.Returns(testBtnRules)
.Verifiable();
var controller = new BtnRulesController(repo.Object);
var result = controller.Get();
Assert.Equal(testBtnRules.Count,result.Count);
repo.Verify();
}

502 error while converting entity framework data to json. Possible recursion. How to prevent it?

[HttpGet("/api/notes/suggested")]
public JsonResult GetSuggestedNotes(string searchText)
{
//TODO: Podpowiedzi przy wpisywaniu tytułu
JsonResult result = null;
try {
List<Note> n = db.Notes.Include(x => x.NoteTags).ToList();
result = Json(n);
}
catch(Exception e)
{
Console.WriteLine(e);
}
return result;
}
public class Note
{
public Note()
{
CreationDate = DateTime.Now;
NoteTags = new HashSet<NoteTag>();
Parts = new HashSet<Part>();
}
public int ID { get; set; }
public virtual ICollection<NoteTag> NoteTags { get; set; }
public virtual ICollection<Part> Parts { get; set; }
public DateTime? CreationDate { get; set; }
[NotMapped]
public string TagsToAdd { get; set; }
[NotMapped]
public string TagsAsSingleString {
get
{
string result = "";
foreach(var nt in NoteTags)
{
result += nt.Tag.Name + " ";
}
return result;
}
}
}
public class NoteTag
{
public int NoteId { get; set; }
public virtual Note Note { get; set; }
public int TagId { get; set; }
public virtual Tag Tag { get; set; }
}
When I try to get data using this WebAPI controller, I get 502 bad gateway. No errors, everything's fine while debugging server. Data get from database correctly.
I suspect that it could be something similar to "infinite loop" but how to prevent it? (Note class is connected to collection of NoteTag objects that are connected back to Note which probably makes this loop).
And why there are no errors if something went wrong? :/
I don't know if it still relevant but i had the same problem and what worked for me it to Configure Newtonsoft.Json
SerializerSettings.ReferenceLoopHandling = ewtonsoft.Json.ReferenceLoopHandling.Ignore.
If you are using VS2015 MVC you can add the following code:
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
in the ConfigureServices method in the Startup class.
I think the problem its recursion, can you try with an Anonymous type
NoteTags has Note , imagine if the Note->NoteTags->Note->NoteTags->Note->NoteTags ...
`List n = db.Notes.Include(x => x.NoteTags).ToList();
var e = n.select(x=> new {property=value});
result = Json(e);`

Find(System.Object[]) cannot be called with instance of type .ObjectQuery [duplicate]

I want to Find Username by userId
this code snippet working
Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName,
and this once not working in following controller class
Comment_CreateBy = db.AspNetUsers.Find(c.CreatedBy).UserName,
this is my model classes
public class DiscussionVM
{
public int Disussion_ID { get; set; }
public string Discussion_Title { get; set; }
public string Discussion_Description { get; set; }
public Nullable<System.DateTime> Discussion_CreateDate { get; set; }
public string Discussion_CreateBy { get; set; }
public string Comment_User { get; set; }
public IEnumerable<CommentVM> Comments { get; set; }
}
public class CommentVM
{
public int Comment_ID { get; set; }
public Nullable<System.DateTime> Comment_CreateDate { get; set; }
public string Comment_CreateBy { get; set; }
public string Comment_Description { get; set; }
}
this is whole controller class
public ActionResult Discussion_Preview()
{
int Discussion_ID = 1;
var discussion = db.AB_Discussion.Where(d => d.Discussion_ID == Discussion_ID).FirstOrDefault();
var comments = db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID);
DiscussionVM model = new DiscussionVM()
{
Disussion_ID = discussion.Discussion_ID,
Discussion_Title = discussion.Discussion_Name,
Discussion_Description = discussion.Discussion_Name,
Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName,
Discussion_CreateDate = discussion.CreatedDate,
Comments = comments.Select(c => new CommentVM()
{
Comment_ID = c.Comment_ID,
Comment_Description = c.Comment_Discription,
Comment_CreateBy = db.AspNetUsers.Find(c.CreatedBy).UserName,
Comment_CreateDate = c.CreatedDate
})
};
return View(model);
}
Getting following error
Method 'Project.Models.AspNetUser Find(System.Object[])' declared on type 'System.Data.Entity.DbSet1[Project.Models.AspNetUser]' cannot be called with instance of type 'System.Data.Entity.Core.Objects.ObjectQuery1[Project.Models.AspNetUser]'
Discussion_CreateBy = db.AspNetUsers.Find(discussion.CreatedBy).UserName
Works because discussion is an in-memory object because you are executing a query by calling FirstOrDefault on it:
var discussion = db.AB_Discussion.Where(d => d.Discussion_ID == Discussion_ID).FirstOrDefault();
On the other hand in the following statement:
db.AspNetUsers.Find(c.CreatedBy).UserName
c is not queried yet because
db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID)
returns an IQueriable and not the actual collection of comments
The easiest way to fix it is to bring all your comments into memory (since you are anyway need them all) :
var comments = db.AB_DiscussionComments.Where(c => c.Discussion_ID == Discussion_ID).ToList();

transfer DTO to ViewModel

Here is my data transfer object
public class LoadSourceDetail
{
public string LoadSourceCode { get; set; }
public string LoadSourceDesc { get; set; }
public IEnumerable<ReportingEntityDetail> ReportingEntity { get; set; }
}
public class ReportingEntityDetail
{
public string ReportingEntityCode { get; set; }
public string ReportingEntityDesc { get; set; }
}
And here is my ViewModel
public class LoadSourceViewModel
{
#region Construction
public LoadSourceViewModel ()
{
}
public LoadSourceViewModel(LoadSourceDetail data)
{
if (data != null)
{
LoadSourceCode = data.LoadSourceCode;
LoadSourceDesc = data.LoadSourceDesc;
ReportingEntity = // <-- ? not sure how to do this
};
}
#endregion
public string LoadSourceCode { get; set; }
public string LoadSourceDesc { get; set; }
public IEnumerable<ReportingEntityViewModel> ReportingEntity { get; set; }
}
public class ReportingEntityViewModel
{
public string ReportingEntityCode { get; set; }
public string ReportingEntityDesc { get; set; }
}
}
I'm not sure how to transfer the data from the LoadSourceDetail ReportingEntity to the LoadSourceViewModel ReportingEntity. I'm trying to transfer data from one IEnumerable to another IEnumerable.
I would use AutoMapper to do this:
https://github.com/AutoMapper/AutoMapper
http://automapper.org/
You can easily map collections, see https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays
It would look something like this:
var viewLoadSources = Mapper.Map<IEnumerable<LoadSourceDetail>, IEnumerable<LoadSourceViewModel>>(loadSources);
If you are using this in an MVC project I usually have an AutoMapper config in the App_Start that sets the configuration i.e. fields that do not match etc.
Without AutoMapper you will have to map each property one by one ,
Something like this :
LoadSourceDetail obj = FillLoadSourceDetail ();// fill from source or somewhere
// check for null before
ReportingEntity = obj.ReportingEntity
.Select(x => new ReportingEntityViewModel()
{
ReportingEntityCode = x.ReportingEntityCode,
ReportingEntityDesc x.ReportingEntityDesc
})
.ToList(); // here is 'x' is of type ReportingEntityDetail
You could point it to the same IEnumerable:
ReportingEntity = data.ReportingEntity;
If you want to make a deep copy, you could use ToList(), or ToArray():
ReportingEntity = data.ReportingEntity.ToList();
That will materialize the IEnumerable and store a snapshot in your view model.

Categories

Resources