I am developing an MVC4 application with Angular JS and Entity Framework6.0. While saving data to DB its save only in One table. Actually it would be save into different table. Lets say Patient, PatientAddress, City,State etc.
Using EF so its creates Model Like this
[GeneratedCodeAttribute("EF.Reverse.POCO.Generator", "2.17.0.0")]
public partial class Patient
{
public int PatientId { get; set; } // PatientID (Primary key)
public Guid? PatientGuid { get; set; } // PatientGUID
public string FirstName { get; set; } // FirstName
public string MiddleName { get; set; } // MiddleName
public string LastName { get; set; } // LastName
public DateTime? DateOfBirth { get; set; } // DateOfBirth
public int? Age { get; set; } // Age
public int GenderId { get; set; } // GenderID
public string Occupation { get; set; } // Occupation
// Reverse navigation
public virtual ICollection<PatientAddress> PatientAddresses { get; set; } // Many to many mapping
public virtual ICollection<PatientAppointment> PatientAppointments { get; set; } // PatientAppointment.fk_PatientAppointment_Patient
public virtual ICollection<PatientInsurance> PatientInsurances { get; set; } // Many to many mapping
public virtual PatientEmergencyContact PatientEmergencyContact { get; set; } // PatientEmergencyContact.fk_PatientEmergencyContact_Patient
// Foreign keys
public virtual GenderMaster GenderMaster { get; set; } // fk_Patient_GenderMaster
public Patient()
{
PatientGuid = System.Guid.NewGuid();
PatientAddresses = new List<PatientAddress>();
PatientAppointments = new List<PatientAppointment>();
PatientInsurances = new List<PatientInsurance>();
InitializePartial();
}
partial void InitializePartial();
}
}
AngularJS Having Controller.Js here I get html form values
app.controller("PatientInfo", function ($scope, myService) {
$scope.SavePatient = function () {
debugger;
// alert("Red");
var PatientInformation = {
FirstName: $scope.Patient.FirstName,
MiddleName: $scope.Patient.MiddleName,
LastName: $scope.Patient.LastName,
DOB: $scope.Patient.DOB,
Age: $scope.Patient.Age,
GenderId : $scope.Patient.GenderId,
Occupation: $scope.Patient.Occupation,
Address: $scope.PatientAddress.AddressPa,
District: $scope.CityMaster.NameCm,
State: $scope.StateMaster.NameSm,
Pincode: $scope.PatientAddress.PinCodePa,
PhoneNumber: $scope.PatientAddress.PhoneNumberPa,
EmailID: $scope.PatientAddress.EmailID,
ContactPersonName: $scope.ContactPersonName,
ContactPersonRelation: $scope.ContactPersonRelation,
ContactPersonAddress: $scope.ContactPersonAddress,
ContactPersonEmail: $scope.ContactPersonEmail,
ContactPersonPhoneNumber: $scope.ContactPersonPhoneNumber,
InsuranceID: $scope.InsuranceID
};
var getData = myService.AddPatient(PatientInformation);
getData.then(function (msg) {
alert(msg.data);
}, function () {
alert('Error in adding record');
});
}
});
Service.JS: and here I pass above controller value to MVC Controller.
app.service("myService", function ($http) {
this.AddPatient = function (PatientInformation) {
debugger;
var response = $http({
method: "post",
url: "RegisterNewPatient/AddPatient",
data: JSON.stringify(PatientInformation),
dataType: "json"
});
return response;
}
});
RegisterNewPatientController.cs
public class RegisterNewPatientController : Controller
{
HospitalTeamWebDeContext objHospitalTeamWebDeContext = new HospitalTeamWebDeContext();
public ActionResult Index()
{
return View("..//Patient/RegisterNewPatient");
}
public string AddPatient(Patient objPatient)
{
try
{
if (objPatient.GenderId == 0)
{
objPatient.GenderId = 1;
}
objHospitalTeamWebDeContext.Patients.Add(objPatient);
objHospitalTeamWebDeContext.SaveChanges();
}
catch
{
}
finally
{
}
return "Patient Saved.";
}
}
}
Here is above code.
Now It time to show you the o/p
But In controller.CS file I get an only Patient information but Pateint Address, Patient Emergency Contact are always blank.

I have tried lot of approach for it but it wont work.

public virtual ICollection<PatientAddress> PatientAddressesas with data coming from js.