Reputation: 38564
I'm trying to follow the Contoso University tutorial, only with SQL Server 2008 instead of CE. I can connect to the database engine in SQL Server Management Studio using my PC's name and windows authentication. I haven't created a database with it because I'm trying to use EF Code First.
This is my connection string:
<add
name="SchoolContext"
connectionString="Data Source=CARSON-PC\CARSON;Integrated Security=true"
providerName="System.Data.SqlClient"/>
I try to add a controller by right-clicking the controllers folder and selecting Add->Controller...
and setting up the resulting dialog according to the tutorial. I wind up with a dialog telling me a FileNotFoundException
was thrown, and the file is a temporary dll:
This dialog is thrown up five times (once for each of the generated template files, I assume) and I wind up with a controller but no generated templates.
I suspect it's to do with either my connection string or my SQL server installation, since I've been stuck on this a while, getting various errors at this stage as I try and get the connection string right.
For completeness, here's the model I'm trying to generate a controller for:
public class Student {
public int StudentID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
And the context:
public class SchoolContext : DbContext {
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(modelBuilder As DbModelBuilder) {
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Is my connection string wrong? Is this a symptom of something not configured right with the server? What am I screwing up here?
Upvotes: 3
Views: 896
Reputation: 38564
This turned out to be a simple bug in the MVC template files List.tt, Edit.tt, Details.tt, etc. There was an ambiguous reference to ColumnAttribute
.
I opened C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML
, and going through each of the .tt
files and searching for " ColumnAttribute" (with the space at the beginning):
//...
var column = attribute as ColumnAttribute;
if (column != null && column.IsPrimaryKey) { // LINQ to SQL
return true;
}
//...
I changed that first line to:
var column = attribute as System.Data.Linq.Mapping.ColumnAttribute;
I think only Empty.tt didn't have it. I did the same for the VB templates. Now the controllers are created fine.
Upvotes: 2
Reputation: 1404
I have just tested this with ASP.NET MVC 4, SQL SERVER 2008 Express. Every thing works. You need to give the database name in connection string, which is used by EF to automatically create database with this name if you have this type code in global.asax,
System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<SchoolContext>());
Here is my connection string,
<add name="SchoolContext" connectionString="Data Source=.\SQlEXPRESS;Initial Catalog=SchoolDatabase;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />
Make sure,
Build the application before use.
Try to run the application as an administrator.
You don't need to create SchoolContext initially, the wizard will automatically a one for you.
A simple way to find the connection string is to use Server Explorer.
Upvotes: 2