WebApi Controller Method with SimpleInjector not adding records to database












0















On my form I am using ajax to submit the form to my API controller method for creating new objects. In my API controller, I am using SimpleInjector for Dependency Injection but for some reason when that method is hit, my object isn't being added/saved to the database table. I am not receiving any runtime errors and it debugs perfectly.



Here is my code:



// POST: api/MedicInfoesApi
[ResponseType(typeof(MedicInfo))]
public IHttpActionResult PostMedicInfo(MedicInfo medicInfo)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

// Create empty Employee Object to get info of person being submitted via IBM
Employee emp = new Employee();

//check if IBM that user is submitting exists
if (!EmployeeData.IsValidIBM(medicInfo.MedicIbm))
{
ModelState.AddModelError("", "This IBM does not exist!");
}
// Check if any existing IBM's match what the user is trying to submit... if none then save to database
else if (_dbContext.GainAccess().MedicInfoes.Any(x => x.MedicIbm.Equals(medicInfo.MedicIbm, StringComparison.CurrentCultureIgnoreCase)))
{
ModelState.AddModelError("", "This person already exists!");
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
// Set empty Employee object with Data of person
emp = EmployeeData.GetEmployee(medicInfo.MedicIbm);
medicInfo.Active = true;
_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // no errors but nothing is added
_dbContext.GainAccess().SaveChanges(); // no errors but nothing saves
}

return CreatedAtRoute("DefaultApi", new { id = medicInfo.Id }, medicInfo);
}









share|improve this question

























  • "my object isn't being added/saved to the database" - how do you determine that? Are you looking at the same database as your application writes to?

    – CodeCaster
    Nov 19 '18 at 14:56











  • @CodeCaster yes I am. When the method fully executes, I execute the table that this is supposed to write to, and it remains empty. Plus, on completion of my ajax, it redirects to a page that retrieves all of the objects from that table, and that page is empty

    – M12 Bennett
    Nov 19 '18 at 14:59






  • 1





    So what actually is the GainAccess() method?

    – CodeCaster
    Nov 19 '18 at 15:31
















0















On my form I am using ajax to submit the form to my API controller method for creating new objects. In my API controller, I am using SimpleInjector for Dependency Injection but for some reason when that method is hit, my object isn't being added/saved to the database table. I am not receiving any runtime errors and it debugs perfectly.



Here is my code:



// POST: api/MedicInfoesApi
[ResponseType(typeof(MedicInfo))]
public IHttpActionResult PostMedicInfo(MedicInfo medicInfo)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

// Create empty Employee Object to get info of person being submitted via IBM
Employee emp = new Employee();

//check if IBM that user is submitting exists
if (!EmployeeData.IsValidIBM(medicInfo.MedicIbm))
{
ModelState.AddModelError("", "This IBM does not exist!");
}
// Check if any existing IBM's match what the user is trying to submit... if none then save to database
else if (_dbContext.GainAccess().MedicInfoes.Any(x => x.MedicIbm.Equals(medicInfo.MedicIbm, StringComparison.CurrentCultureIgnoreCase)))
{
ModelState.AddModelError("", "This person already exists!");
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
// Set empty Employee object with Data of person
emp = EmployeeData.GetEmployee(medicInfo.MedicIbm);
medicInfo.Active = true;
_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // no errors but nothing is added
_dbContext.GainAccess().SaveChanges(); // no errors but nothing saves
}

return CreatedAtRoute("DefaultApi", new { id = medicInfo.Id }, medicInfo);
}









share|improve this question

























  • "my object isn't being added/saved to the database" - how do you determine that? Are you looking at the same database as your application writes to?

    – CodeCaster
    Nov 19 '18 at 14:56











  • @CodeCaster yes I am. When the method fully executes, I execute the table that this is supposed to write to, and it remains empty. Plus, on completion of my ajax, it redirects to a page that retrieves all of the objects from that table, and that page is empty

    – M12 Bennett
    Nov 19 '18 at 14:59






  • 1





    So what actually is the GainAccess() method?

    – CodeCaster
    Nov 19 '18 at 15:31














0












0








0


0






On my form I am using ajax to submit the form to my API controller method for creating new objects. In my API controller, I am using SimpleInjector for Dependency Injection but for some reason when that method is hit, my object isn't being added/saved to the database table. I am not receiving any runtime errors and it debugs perfectly.



Here is my code:



// POST: api/MedicInfoesApi
[ResponseType(typeof(MedicInfo))]
public IHttpActionResult PostMedicInfo(MedicInfo medicInfo)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

// Create empty Employee Object to get info of person being submitted via IBM
Employee emp = new Employee();

//check if IBM that user is submitting exists
if (!EmployeeData.IsValidIBM(medicInfo.MedicIbm))
{
ModelState.AddModelError("", "This IBM does not exist!");
}
// Check if any existing IBM's match what the user is trying to submit... if none then save to database
else if (_dbContext.GainAccess().MedicInfoes.Any(x => x.MedicIbm.Equals(medicInfo.MedicIbm, StringComparison.CurrentCultureIgnoreCase)))
{
ModelState.AddModelError("", "This person already exists!");
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
// Set empty Employee object with Data of person
emp = EmployeeData.GetEmployee(medicInfo.MedicIbm);
medicInfo.Active = true;
_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // no errors but nothing is added
_dbContext.GainAccess().SaveChanges(); // no errors but nothing saves
}

return CreatedAtRoute("DefaultApi", new { id = medicInfo.Id }, medicInfo);
}









share|improve this question
















On my form I am using ajax to submit the form to my API controller method for creating new objects. In my API controller, I am using SimpleInjector for Dependency Injection but for some reason when that method is hit, my object isn't being added/saved to the database table. I am not receiving any runtime errors and it debugs perfectly.



Here is my code:



// POST: api/MedicInfoesApi
[ResponseType(typeof(MedicInfo))]
public IHttpActionResult PostMedicInfo(MedicInfo medicInfo)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

// Create empty Employee Object to get info of person being submitted via IBM
Employee emp = new Employee();

//check if IBM that user is submitting exists
if (!EmployeeData.IsValidIBM(medicInfo.MedicIbm))
{
ModelState.AddModelError("", "This IBM does not exist!");
}
// Check if any existing IBM's match what the user is trying to submit... if none then save to database
else if (_dbContext.GainAccess().MedicInfoes.Any(x => x.MedicIbm.Equals(medicInfo.MedicIbm, StringComparison.CurrentCultureIgnoreCase)))
{
ModelState.AddModelError("", "This person already exists!");
}

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
// Set empty Employee object with Data of person
emp = EmployeeData.GetEmployee(medicInfo.MedicIbm);
medicInfo.Active = true;
_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // no errors but nothing is added
_dbContext.GainAccess().SaveChanges(); // no errors but nothing saves
}

return CreatedAtRoute("DefaultApi", new { id = medicInfo.Id }, medicInfo);
}






c# asp.net-web-api simple-injector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 1:00









Tetsuya Yamamoto

16.1k42240




16.1k42240










asked Nov 19 '18 at 14:54









M12 BennettM12 Bennett

3,55511854




3,55511854













  • "my object isn't being added/saved to the database" - how do you determine that? Are you looking at the same database as your application writes to?

    – CodeCaster
    Nov 19 '18 at 14:56











  • @CodeCaster yes I am. When the method fully executes, I execute the table that this is supposed to write to, and it remains empty. Plus, on completion of my ajax, it redirects to a page that retrieves all of the objects from that table, and that page is empty

    – M12 Bennett
    Nov 19 '18 at 14:59






  • 1





    So what actually is the GainAccess() method?

    – CodeCaster
    Nov 19 '18 at 15:31



















  • "my object isn't being added/saved to the database" - how do you determine that? Are you looking at the same database as your application writes to?

    – CodeCaster
    Nov 19 '18 at 14:56











  • @CodeCaster yes I am. When the method fully executes, I execute the table that this is supposed to write to, and it remains empty. Plus, on completion of my ajax, it redirects to a page that retrieves all of the objects from that table, and that page is empty

    – M12 Bennett
    Nov 19 '18 at 14:59






  • 1





    So what actually is the GainAccess() method?

    – CodeCaster
    Nov 19 '18 at 15:31

















"my object isn't being added/saved to the database" - how do you determine that? Are you looking at the same database as your application writes to?

– CodeCaster
Nov 19 '18 at 14:56





"my object isn't being added/saved to the database" - how do you determine that? Are you looking at the same database as your application writes to?

– CodeCaster
Nov 19 '18 at 14:56













@CodeCaster yes I am. When the method fully executes, I execute the table that this is supposed to write to, and it remains empty. Plus, on completion of my ajax, it redirects to a page that retrieves all of the objects from that table, and that page is empty

– M12 Bennett
Nov 19 '18 at 14:59





@CodeCaster yes I am. When the method fully executes, I execute the table that this is supposed to write to, and it remains empty. Plus, on completion of my ajax, it redirects to a page that retrieves all of the objects from that table, and that page is empty

– M12 Bennett
Nov 19 '18 at 14:59




1




1





So what actually is the GainAccess() method?

– CodeCaster
Nov 19 '18 at 15:31





So what actually is the GainAccess() method?

– CodeCaster
Nov 19 '18 at 15:31












1 Answer
1






active

oldest

votes


















3














I have a suspicion that GainAccess() may be returning a new instance of your context every time it is called, which could explain the behavior.



_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // This instance is never saved
_dbContext.GainAccess().SaveChanges(); // This instance has nothing to save


You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other.



In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.



Try using the same instance for the operation.



medicInfo.Active = true;
var context = _dbContext.GainAccess(); //what ever it returns.
context.MedicInfoes.Add(medicInfo);
context.SaveChanges();





share|improve this answer


























  • That worked! Can you go a little further in explanation?

    – M12 Bennett
    Nov 19 '18 at 15:16






  • 1





    @M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

    – Nkosi
    Nov 19 '18 at 15:17













  • Understood. Thank you!

    – M12 Bennett
    Nov 19 '18 at 15:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377210%2fwebapi-controller-method-with-simpleinjector-not-adding-records-to-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














I have a suspicion that GainAccess() may be returning a new instance of your context every time it is called, which could explain the behavior.



_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // This instance is never saved
_dbContext.GainAccess().SaveChanges(); // This instance has nothing to save


You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other.



In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.



Try using the same instance for the operation.



medicInfo.Active = true;
var context = _dbContext.GainAccess(); //what ever it returns.
context.MedicInfoes.Add(medicInfo);
context.SaveChanges();





share|improve this answer


























  • That worked! Can you go a little further in explanation?

    – M12 Bennett
    Nov 19 '18 at 15:16






  • 1





    @M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

    – Nkosi
    Nov 19 '18 at 15:17













  • Understood. Thank you!

    – M12 Bennett
    Nov 19 '18 at 15:26
















3














I have a suspicion that GainAccess() may be returning a new instance of your context every time it is called, which could explain the behavior.



_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // This instance is never saved
_dbContext.GainAccess().SaveChanges(); // This instance has nothing to save


You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other.



In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.



Try using the same instance for the operation.



medicInfo.Active = true;
var context = _dbContext.GainAccess(); //what ever it returns.
context.MedicInfoes.Add(medicInfo);
context.SaveChanges();





share|improve this answer


























  • That worked! Can you go a little further in explanation?

    – M12 Bennett
    Nov 19 '18 at 15:16






  • 1





    @M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

    – Nkosi
    Nov 19 '18 at 15:17













  • Understood. Thank you!

    – M12 Bennett
    Nov 19 '18 at 15:26














3












3








3







I have a suspicion that GainAccess() may be returning a new instance of your context every time it is called, which could explain the behavior.



_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // This instance is never saved
_dbContext.GainAccess().SaveChanges(); // This instance has nothing to save


You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other.



In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.



Try using the same instance for the operation.



medicInfo.Active = true;
var context = _dbContext.GainAccess(); //what ever it returns.
context.MedicInfoes.Add(medicInfo);
context.SaveChanges();





share|improve this answer















I have a suspicion that GainAccess() may be returning a new instance of your context every time it is called, which could explain the behavior.



_dbContext.GainAccess().MedicInfoes.Add(medicInfo); // This instance is never saved
_dbContext.GainAccess().SaveChanges(); // This instance has nothing to save


You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other.



In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.



Try using the same instance for the operation.



medicInfo.Active = true;
var context = _dbContext.GainAccess(); //what ever it returns.
context.MedicInfoes.Add(medicInfo);
context.SaveChanges();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 15:21

























answered Nov 19 '18 at 15:05









NkosiNkosi

115k16128193




115k16128193













  • That worked! Can you go a little further in explanation?

    – M12 Bennett
    Nov 19 '18 at 15:16






  • 1





    @M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

    – Nkosi
    Nov 19 '18 at 15:17













  • Understood. Thank you!

    – M12 Bennett
    Nov 19 '18 at 15:26



















  • That worked! Can you go a little further in explanation?

    – M12 Bennett
    Nov 19 '18 at 15:16






  • 1





    @M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

    – Nkosi
    Nov 19 '18 at 15:17













  • Understood. Thank you!

    – M12 Bennett
    Nov 19 '18 at 15:26

















That worked! Can you go a little further in explanation?

– M12 Bennett
Nov 19 '18 at 15:16





That worked! Can you go a little further in explanation?

– M12 Bennett
Nov 19 '18 at 15:16




1




1





@M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

– Nkosi
Nov 19 '18 at 15:17







@M12Bennett You are calling your Add and Save on completely different instances of your context. So those method calls are independent of each other. In the first instance you add a record but never save to the database. you then create another new instance and then tell it to save changes, but it has no changes to save because it is a new instance with nothing done to it.

– Nkosi
Nov 19 '18 at 15:17















Understood. Thank you!

– M12 Bennett
Nov 19 '18 at 15:26





Understood. Thank you!

– M12 Bennett
Nov 19 '18 at 15:26




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377210%2fwebapi-controller-method-with-simpleinjector-not-adding-records-to-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini