Reputation: 2189
I am using MVC first time, and I got this error when running the application.
Here are my steps.
MVC
empty ProjectMovieReview Class is this
And MovieReviewContext Class is this
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.
Upvotes: 1
Views: 958
Reputation: 39
Your written code fine and there is no problem.
Check DB user permission which have or not to create database
Hopefully this work fine
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 1
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
}
Upvotes: 0
Reputation: 14640
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>
Upvotes: 3
Reputation: 683
As the message indicates you'll need to give CREATE DATABASE permissions to the user that you've specified in your web.config file.
Upvotes: 1
Reputation: 53958
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.
Upvotes: 2