Harsh Singhi
Harsh Singhi

Reputation: 117

Retrieve data from sql in MVC 4

Platfrom : MVC4

Hi, I am new to MVC4 and I am trying to retrieve the data from SQL using Entity framework, but it fails. Here is the code I have tried so far :

Model: MovieListing.cs

[Table("MovieMaster")]
public class MoviesListing 
{
    public int MovieId { get; set; }
    public string Name { get; set; }
    public string year { get; set; }
    public string Image { get; set; }
    public string producer { get; set; }
    public string plot { get; set; }
}

Model:

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

View :

@model MovieManiac.Models.MoviesListing

@{
    ViewBag.Title = "Moviedetails";
}

<h2>Moviedetails</h2>

<table>
    <tr>
        <td>Movie Name</td>
        <td> @Model.Moviename</td>
    </tr>


</table>

Controller:

public ActionResult MovieDetails(int id)
        {

                MovieContext objMovies = new MovieContext();
                MoviesListing movie = objMovies.Movies.Single(mov => mov.MovieId == id);

                return View(movie);


        }

So, in the browser, when I enter the Id, I want to retrieve information from database.I am using local sql db. Any suggestions?

Connection String:

</configSections>
  <connectionStrings>
    <add name="MovieContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MovieManiac-20150318225934;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MovieManiac-20150318225934.mdf" />
  </connectionStrings>

error:

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'MoviesListing' has no key defined. Define the key for this EntityType.

\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Movies' is based on type 'MoviesListing' that has no keys defined.

Upvotes: 1

Views: 257

Answers (2)

matt_lethargic
matt_lethargic

Reputation: 2786

You need to add the Key attribute to the I'd property so EF knows what is the primary key

https://msdn.microsoft.com/en-us/data/jj591583.aspx

http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx

Update

You may be able to use int and numeric, although I wouldn't bother myself, just change your model to decimal to match the datatype mapping... https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx... if you have a need to use numeric.

Also decorate your class with the Key attribute

[Table("MovieMaster")]
public class MoviesListing 
{
    [Key]
    public int MovieId { get; set; }
    public string Name { get; set; }
    public string year { get; set; }
    public string Image { get; set; }
    public string producer { get; set; }
    public string plot { get; set; }
}

You can read about using a decimal (numeric) as a key http://dotnetwindow.blogspot.co.uk/2012/06/using-decimal-as-primary-key-in-entity.html

BTW, are you using code first?

Upvotes: 1

Mahesh
Mahesh

Reputation: 567

    add property to your entity 
    [Table("MovieMaster")]
    public class MoviesListing 
    {
        public int Id { get; set; }      // add this
        public int MovieId { get; set; }
        public string Name { get; set; }
        public string year { get; set; }
        public string Image { get; set; }
        public string producer { get; set; }
        public string plot { get; set; }
    }

or else use this
public class MovieContext:DbContext
    {
        public DbSet<MoviesListing> Movies { get; set;  }

 protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MoviesListing>.HasKey(x => x.MovieId );
        base.OnModelCreating(modelBuilder);
    }
    }

Upvotes: 0

Related Questions