0

Hello guys, totally new to MVC and I've built a small simple application with the help of a book I'm reading. But, needless to say it, I run into some problems. Let me run through what I did and what the problem is (by the way, I'm not so sure how easy it will be to show you the code, I will probably add everything to pastebin...funny they say the MVC application are easier to test...um...not so sure about that).

ANyway, my project is called Storage_test and it is nothing else than a form that allows users to input some data in a database and then retrieve them on the same page.

Now, let me say that while I appreciate this can be done in more effective ways, as said I've just started and found this method fairly easy, so I rather fix this than finding a better one.

So, first of all, I created a database, named Storage_test.sdf and the table in it Storage_test_records (the book suggested to do that with SQL Server Compact and so I did), here is the screenshot: http://s10.postimg.org/n8em2mw3t/Storage_test_records.png

Then I proceeded to create a model class StorageRecord.cs which essentially is a representation of the storage record which will go in the database, here http://pastebin.com/FT1s40wk

Then a StorageRecordContext.cs that contains the DbContex info to use the Entity Framework info (Which the book made me install as an add on to Visual studio 2012), here http://pastebin.com/xqaH6XqE

And a controller called StorageController.css here http://pastebin.com/YYBhiQYN that has 2 versions of Create method (one that simply returns a view and the other one that upload the content to the database) and an Index one.

Needless to say, I've created the relative Views: Create - which contains the form - http://pastebin.com/Adgs6Jvb and Index that takes care of displaying the data stored on the database http://pastebin.com/VXuLUvRx

So, here is the thing: When I navigate to Storage/Create I have the form to fill in, and when I submit it, the page returns the Index method which is supposed to show me what I have submitted to the database. It all works, as you can see from here http://s22.postimg.org/ahj6n9i5d/index.png, but if I go into my application and check the table in the database, it is empty. At first, I thought, well you know I need to refresh it, so did that and even closed the app down and resubmitted again, but the table is still empty. Clearly the data submitted is going somewhere, but where the heck? It is supposed to be in that table on that database, and yet, it's not. Surely I'm doing something stupid, considering that I have first created an example in the book about a very similar program which works fine and has no issues with the database table at all, but this one I built has, and I'm going insane trying to understand why. In my StorageRecordContext.css I'm passing the name of the database to the base class (DbContext) with this line public StorageRecordContext() : base("Storage_test") { }, so shouldn't that be enough to guarantee that the data inserted go in fact in that database?

Any help, is as usual, is much appreciated

thanks

2
Contributors
4
Replies
56
Views
2 Years
Discussion Span
Last Post by Violet_82
0

Hi

Silly question but will ask anyway, is the connection string correctly pointing to your database in the App_Data folder. Your connection string is usually set within your web.config file (not the one in the Views folder but the one in the root folder) and you will usually have a Default one and potentially an additional one.

0

It's not a silly question at all. In fact, I have no idea, and I'll explain why. The exercise detailed on the book, is essentially the same as mine, but the project name and all the file names are different. Theirs works, and when I look at the database created I can see the data in it. At no point they have modified the connection string.
I then reused pretty much the same code for a different project, and obviously I haven't touched the connection string. The assumption, I presume, was that something else will take care of it, as it appears to have done with their own exercise. Something obviously has gone wrong.
I'll have a look, so the string needs to point to the database I created correct, presumably it has its name on in in a string format?

0

Hi

If I remember rightly, their example is the Movie database using the Entity Framework with a Code First approach. The reason that works is because of this Code First approach as Entity Framework will create the database and tables based on the Models in the application the first time the application is run. The default connection defined in the Web.Config file when the project is first setup uses a localDb instance and it is this that Entity Framework will then use. So you will probably find that your data is in a table within that database.

If you have created your own Models and DBContext class then you need to add a new connection string to the Web.Config file with the same name as your DBContext class. So assuming you had created a class called StorageDbContext then in the Web.Config you would add a new entry to the ConnectionStrings section like:

<add name="StorageDbContext" connectionString="your connection string" ProviderName="System.Data.SqlClient" />

You can copy the connection string from the Default one but make sure that it points to the right database.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.