Click here to Skip to main content
15,891,567 members

How save records when calling savechanges() in entityframwork?

ExpertITM asked:

Open original thread
Hello,

I am Creating MVC Application MvcMovies by guiding Asp.net tutorial(http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model's-data-from-a-controller[^])

I have created sql server database and created a table Movies,

my only question is that where to store data after called savechanges()

if you want code than,

Below is my code model controller and view,

Movies.cs

C#
public class Movies
    {
        public int id { get; set; }
        public string title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal price { get; set; }
        public string Ratings { get; set; }

    }

    public class MoviesDBContext : DbContext
    {
        public DbSet<Movies> Movies { get; set; }
    }


MoviesController.cs

C#
//
       // GET: /Movies/Create

       public ActionResult Create()
       {
           return View();
       }

       //
       // POST: /Movies/Create

       [HttpPost]
       public ActionResult Create(Movies movies)
       {
           if (ModelState.IsValid)
           {
               db.Movies.Add(movies);
               db.SaveChanges();
               return RedirectToAction("Index");
           }

           return View(movies);
       }


create.cshtml
HTML
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Movies</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.title)
            @Html.ValidationMessageFor(model => model.title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ReleaseDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ReleaseDate)
            @Html.ValidationMessageFor(model => model.ReleaseDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Genre)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Genre)
            @Html.ValidationMessageFor(model => model.Genre)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.price)
            @Html.ValidationMessageFor(model => model.price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Ratings)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Ratings)
            @Html.ValidationMessageFor(model => model.Ratings)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}


I need to know that when I am creating new record where it will be inserted.

I have checked database table but there is no records.

Thanks.
Tags: C#, MVC, ASP.NET, SQL Server, SQL Server 2008

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900