public int CreateFromTemplate(int claimTemplateId) { var claimTemplateController = new ClaimTemplateController(); var mytemplate = claimTemplateController.GetClaimTemplate(claimTemplateId); var currentUser = _userFactory.GetCurrentUser(); var claim = new Claim { ClaimTemplateID = claimTemplateId, CreatedBy = User.Identity.Name, DateCreated = DateTime.Now, UserID = currentUser.UserId, ClaimStatusID = 1, WillisEmployeeID = 1 }; _db.Claims.Add(claim); string claimName = claim.CreatedBy + " - " + claim.ClaimTemplateID + " - " + ((claim.DateCreated != null) ? (claim.DateCreated.Value).ToShortDateString() : String.Empty); claim.Name = claimName.Length < 50 ? claimName : claimName.Substring(0, 50); _db.SaveChanges(); var claimId = claim.ClaimID; // here yo foreach (var claimFieldGroupTemplate in mytemplate.ClaimFieldGroupTemplates) { var claimFieldGroup = new ClaimFieldGroup { Name = claimFieldGroupTemplate.Name, Description = claimFieldGroupTemplate.Description, ItemOrder = claimFieldGroupTemplate.ItemOrder }; foreach (var claimFieldTemplate in claimFieldGroupTemplate.ClaimFieldTemplates) { var claimField = new ClaimField { Name = claimFieldTemplate.Name, Code = claimFieldTemplate.Code, ClaimFieldTemplateID = claimFieldTemplate.ClaimFieldTemplateID, ClaimFieldGroupID = claimFieldGroupTemplate.ClaimFieldGroupTemplateID, TemplateBName = claimFieldTemplate.FieldType.TemplateName }; claimFieldGroup.ClaimFields.Add(claimField); } claim.ClaimFieldGroups.Add(claimFieldGroup); } _db.SaveChanges(); _db.Dispose(); return(claimId); }