1

I am migrating /re-developing a web app from JavaScript to the ASP.NET MVC Framework using C#/ JS (with Handlebars.NET) for my Bachelor thesis.

So far I have created a Web.API and the actual app with a form. In the app I enter details to create a new Employee, which is then Posted to the API, which receives that Json-Object as a "Business Object" BOEmployee.

Said BOEmployee looks like this (simplified):

public class BOEmployee
    {
        public int ID_Employee { get; set; }
        public int ID_Company { get; set; }
        public string lastName { get; set; }
        public string firstName { get; set; }
}

I want to map this object to two other objects, representing tables of the underlying database, to then save them to the database. The two target tables are auto generated with Entity Framework.

Here are the table objects:

1. Employee:

public partial class Employee
   {    
        public int ID_Employee { get; set; }
        public int ID_Company { get; set; }
}

2. Employee_Details:

  public partial class Employee_Detail
    {
        public int ID_Employee_Detail { get; set; }
        public int ID_Employee { get; set; }
        public string lastName { get; set; }
        public string firstName { get; set; }
}

Now I could map them manually by assigning every attribute but clearly that is a horribly unsustainable idea. So I was looking for a way to automate that mapping process automatically using Json.Net like this:

[HttpPost]
public BOEmployee SaveEmployee([FromBody] string employee)
{
    using (var context = new myDBEntities())
          {
               JavaScriptSerializer serializer = new JavaScriptSerializer();
    
               Employee_Detail dbEmployeeDetails = serializer.Deserialize<Employee_Detail>(BOEmployee);
               Employee dbEmployee = serializer.Deserialize<Employee>(BOemployee);
    }
}

Now what happens when I run that code is, that the serializer-function complains that the input values cannot be null, which to my understanding is because the target Objects (e.g. Employee) do not have all attributes that are given in the serialized Json-Object.

The Error Message is this:

Value cannot be null.\r\nParameter name: input",

"ExceptionType":"System.ArgumentNullException"

Now my question would be, how can I map my object to the different Database tables? Or am I completely on the wrong path now?

Fundamental changes to the program structure cannot be made any more due to available time (and I am basically a complete beginner in programming).

1 Answer 1

1

I recommend AutoMapper than what you are using there.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. An Automapper won't convert that Json-String to a C# Object though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.