Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

MVC DbContext will not save...

  • 25-06-2014 10:28am
    #1
    Closed Accounts Posts: 1,143 ✭✭✭


    Hi Folks,

    I'm a newbie to MVC and only recently have my head around Views and Controllers and Models at the very basic level, although I've good experience developing with Webforms before I turned my hand to MVC.

    I have migrated some functionality that I have on my webforms (Desktop) site, it is basically a page that connects to my suppliers website, via a HTTP request, I put in a stock code/part number and I get back a few fields relating to that product item.

    I have this working fine on my MVC solution, as in I put in a stock code and I get a HTTP response, and my View is populated with the fields in the response.

    The issue is that I want to save each response to my DB for audit log purposes, and this fairly simple task is proving impossible to accomplish. To complicate matters, I'm new at this and finding it frustrating that if I change my model at all, I'm running into this "The Model has been changed since the DB was first created", error. I've restored my solution back to where it was before I got this error, my solution is running now and my HTTP service between my website and my suppliers, is working fine, the only thing that isn't working is that my DbContext is not saving to my DB as I would expect and there are no errors being generated to say what the issue might be...


    Strangely, the data in my StockDB is being returned in my View, but is not being saved to my DB? And if I try to change my Model at all, it says it has changed since my DB was created, so I'm assuming it is connecting back to my DB and checking the table column names and that they match my Model? :confused::confused::confused:

    Thanks for any help with this headwrecker problem!


Comments

  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Regarding the Model changing error messages, you need to look into Entity Framework migrations. You will want to use explicit migrations or automatic migrations.

    Regarding the not saving, have you confirmed that the Add and SaveChanges is being called? As in, have you confirmed that there isn't Error elements in that request you make?


  • Closed Accounts Posts: 1,143 ✭✭✭LordNorbury


    Regarding the Model changing error messages, you need to look into Entity Framework migrations. You will want to use explicit migrations or automatic migrations.

    Regarding the not saving, have you confirmed that the Add and SaveChanges is being called? As in, have you confirmed that there isn't Error elements in that request you make?
    Hi Procrastinator,

    I just went back and changed my Model so that it isn't giving me the error, as I haven't the time right now to delve into Entity Framework migrations, etc. But if I'm not getting that error, I'm assuming (I'm hoping correctly!), that my Model and my DB match, and that my solution is going back to my DB table and is checking that this is the case, that basically there are columns in my DB table that match what is in my Model.

    What I have done is that when no data is returned, as per my solution, the view doesn't load. When it does load stock data from the HTTP request, then I can see the data in my View as the conditions are met for 'ViewBag.Show = True;", in my Controller.

    I'm reasoning that if the 'ViewBag.Show = True' command is being executed (as in that I am seeing the required data in my View), then surely the next two lines of code below that deal with my Stock Data (StockDB) being added to my DbContext (sDB) and the sDB.SaveChanges(); being executed, this code is surely being executed if my ViewBag.Show = True; is being executed, (as I am seeing the data in my view?)...


    sDB.StockDBs.Add(StockDB);
    sDB.SaveChanges();


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    How are you checking whether the data is saved?

    I would also say check your connection string. Visual studio packs it own version of SQL Express, so it's possible it saving to another DB then the one you expect.


  • Closed Accounts Posts: 1,143 ✭✭✭LordNorbury


    How are you checking whether the data is saved?

    I would also say check your connection string. Visual studio packs it own version of SQL Express, so it's possible it saving to another DB then the one you expect.

    I have MS Server Management Studio 2012 so I go connect to my remote DB on my hosting server so I open up a connection and go find my table and right click on it (edit last 1000 rows), and there are no rows in the table, so I'm assuming there is no data being posted... Will check though in case a DB was created locally although I think I checked this...


  • Closed Accounts Posts: 1,143 ✭✭✭LordNorbury


    I've managed to sort it thankfully, the issue was my connectionstring, I needed to rename it to match my DB Context name it would appear...

    Thanks a mil procrastinator for the suggestions...


  • Advertisement
  • Administrators Posts: 54,420 Admin ✭✭✭✭✭awec


    If things aren't saving and you aren't getting errors always double check your connectionstring. :)


  • Closed Accounts Posts: 1,143 ✭✭✭LordNorbury


    awec wrote: »
    If things aren't saving and you aren't getting errors always double check your connectionstring. :)

    Am I correct in saying that my ConnectionString Name must match by DB Context name for everything to hook up together?!?


  • Closed Accounts Posts: 1,143 ✭✭✭LordNorbury


    I even got my Migrations set up, I do find it strange though that this somewhat obsessive matching between the DB and the Model, is forced on a developer using MVC, (I'm very new to MVC). I'm sure there is a reason for it for more complex relational situations but using Webforms, it seemed to be that the developer could be trusted to make sure that whatever data he/she was trying to access programmatically, that there was an associated column in the DB, or if there wasn't one, that he/she could be trusted to create one?!?


  • Registered Users, Registered Users 2 Posts: 2,040 ✭✭✭Colonel Panic


    That's Entity Framework (and other ORMs are guilty of this) doing not MVC, which doesn't care how you handle the data.


  • Administrators Posts: 54,420 Admin ✭✭✭✭✭awec


    Am I correct in saying that my ConnectionString Name must match by DB Context name for everything to hook up together?!?

    The name of the connectionstring doesn't matter. The default name is DefaultConnection.

    You can pass the name of the connection string you want to the base constructor of your DbContext class.


  • Advertisement
Advertisement