1

I am using MVC first time, and I got this error when running the application. enter image description here

Here are my steps.

  1. Create a new MVC empty Project
  2. Install Entity framework 5.0.0 ( PM> Install-Package EntityFramework -Version 5.0.0 )
  3. Add two classes in Model- MovieReview.cs and MovieReviewContext.cs .

MovieReview Class is this

MovieReview

And MovieReviewContext Class is this enter image description here

  1. In Controllers folder I add a new Controller, named HomeController. Here is itenter image description here .

Then I run it on Google Chrome or on IE. And I got the error I mentioned on the top. Can't understand what to do. Thanks for any help

EDIT I did not change any auto-generated code in any file.

Romo Daneghyan
  • 2,099
  • 3
  • 18
  • 23

6 Answers6

3

Try to add a connection string in the web.config, something like.

  <connectionStrings>
    <add name="MovieReviewContext" 
         connectionString="Data Source=.; Integrated Security= true; Initial Catalog=MovieReviewContext;" 
         providerName="System.Data.SqlClient" />
  </connectionStrings>
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • 2
    If you do not want the name of the connection string to be based on the naming of your context, you can create a constructor which calls the base constructor with the name of a specific connection string. – Björn Boxstart Aug 14 '14 at 13:28
  • Thanks. I added the connection string and it works ^_^ . Thank you. – Romo Daneghyan Aug 14 '14 at 13:49
2

You should check if the user you use for connecting to your SQL server has the permission to create a database in your SQL Server. The error you got says that your user hasn't this permission.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • 2
    Or create a blank database with a user than does have permission and EF will populate it. – DavidG Aug 14 '14 at 13:14
  • @DavidG this is also correct but I didn't mention it, because I thought that the OP uses the code first approach. – Christos Aug 14 '14 at 13:15
  • It's technically still code first if you create the database. Arguably, you could create the entire schema and point the code at it and it's still code-first. :) – DavidG Aug 14 '14 at 13:16
  • Thanks @Christos , one more question please, how can I give that permissions to the user ? – Romo Daneghyan Aug 14 '14 at 13:19
  • @RomoDaneghyan `USE master; GRANT CREATE ANY DATABASE to YourUser;` – DavidG Aug 14 '14 at 13:23
  • @RomoDaneghyan DavidG came first :) ! Follow his approach. I would say the same. – Christos Aug 14 '14 at 13:28
  • Thanks guys. I could not find out where I have to write that statement you mentioned. But I find other solution which is listed in the answers down this question. I added a connection string and it works. Thanks again :) – Romo Daneghyan Aug 14 '14 at 13:50
  • 1
    @RomoDaneghyan you welcome dude ! I am glad that you solved your problem. – Christos Aug 14 '14 at 13:59
1

As the message indicates you'll need to give CREATE DATABASE permissions to the user that you've specified in your web.config file.

vtforester
  • 683
  • 5
  • 7
0
  • Create Empty Application
  • Add your models (Movie, Review, Comment, and Data Access Layer or DDL (repository)
  • Add the Controller
  • Add the view
  • Create your database with your model properties
  • Add connection string in web.config

I would add a model context like this:

public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
}
public class Review
{
    public int Id { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
    public int Id { get; set; }
    public string Title { get; set; }
}

public class MovieReviewService
{
    private MovieContext movieContex;
    private ReviewContext reviewContext;
    private CommentContext commentContext;

    public MovieReviewService(string connectionString)
    {
        this.movieContex = new MovieContext(connectionString);
        this.reviewContext = new ReviewContext(connectionString);
        this.commentContext = new CommentContext(connectionString);
    }

    public void AddMovie(Movie movie)
    {
        this.movieContex.Add(movie);
    }
    public void UpdateMovie(Movie movie)
    {
        this.movieContex.Update(movie);
    }
    public void DeleteMovie(int id)
    {
        this.movieContex.Delete(id);
    }
    public Movie GetMovie(int id)
    {
        return this.movieContex.Get(id);
    }

}

internal class MovieContext : DbContext
{
    private DbSet<Movie> Movies { get { return this.Set<Movie>(); } }

    internal MovieContext(string connectionString)
        : base(connectionString)
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Movie>()
            .HasKey(key => key.Id);

        base.OnModelCreating(modelBuilder);
    }

    internal void Add(Movie movie)
    {
        var existing = this.Movies.Find(movie.Id);

        if (existing == null)
        {
            existing.Id++;
            existing.Title = movie.Title;

            this.Entry<Movie>(existing).State = EntityState.Added;
            this.SaveChanges();
        }
    }
    internal void Update(Movie movie)
    {
        var existing = this.Movies.Find(movie.Id);

        if (existing != null)
        {
            existing.Title = movie.Title;

            this.Entry<Movie>(existing).State = EntityState.Modified;
            this.SaveChanges();
        }
    }
    internal void Delete(int movieId)
    {
        var movie = this.Movies.Find(movieId);

        if (movie != null)
        {
            this.Entry<Movie>(movie).State = EntityState.Deleted;
            this.SaveChanges();
        }
    }
    internal Movie Get(int movieId)
    {
        return this.Movies.Find(movieId);
    }
    internal IEnumerable<Movie> GetAll()
    {
        return this.Movies;
    }
}

internal class ReviewContext : DbContext
{
    private DbSet<Review> Movies { get { return this.Set<Review>(); } }

    internal ReviewContext(string connectionString)
        : base(connectionString)
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Review>()
            .HasKey(key => key.Id)
            .HasMany(comment => comment.Comments);

        base.OnModelCreating(modelBuilder);
    }

    //add your features or action
}

internal class CommentContext : DbContext
{
    private DbSet<Comment> Movies { get { return this.Set<Comment>(); } }

    internal CommentContext(string connectionString)
        : base(connectionString)
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Comment>()
            .HasKey(key => key.Id);

        base.OnModelCreating(modelBuilder);
    }

    //add your features or action
}
Jack
  • 1
  • 1
0

Your application is trying o access data-base, but it's not there. This normally happens when you've change the machine you used to run your application. What I did when I can across that was simply to, "MODIFY DATABASE". Under server explorer you go to your database:

1.right click on it.

2.drop-down menu will occur and click on modify.

3.Connection screen will occur click, OK right on bottom.

4.Another screen will pop-up which will ask you if you want to recreate your data-base

Robert
  • 5,278
  • 43
  • 65
  • 115
0

Your written code fine and there is no problem.

  1. You should check your connection string name which you have written in web.config which should be same as the class name where DbContext is inherited that means your connectionString name should be "MovieReviewContext".
  2. Check DB user permission which have or not to create database

Hopefully this work fine