问题
I am using MVC first time, and I got this error when running the application.
Here are my steps.
- Create a new
MVC
empty Project - Install Entity framework 5.0.0 ( PM> Install-Package EntityFramework -Version 5.0.0 )
- Add two classes in Model- MovieReview.cs and MovieReviewContext.cs .
MovieReview Class is this
And MovieReviewContext Class is this
- In Controllers folder I add a new Controller, named HomeController. Here is it .
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.
回答1:
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>
回答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.
回答3:
As the message indicates you'll need to give CREATE DATABASE permissions to the user that you've specified in your web.config file.
回答4:
- 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
}
回答5:
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
回答6:
Your written code fine and there is no problem.
- 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".
Check DB user permission which have or not to create database
Hopefully this work fine
来源:https://stackoverflow.com/questions/25308791/mvc-getting-error-create-database-permission-denied