I'm following a Entity framework
tutorial: Simple Code First Example it is quite simple, but for some reason it is not working here and it appears the error message is as follows:
AspNet \ Projects \ ENTITY \ ENTITY \ App_Data \ ENTITY.mdf "failed with the operating system error 2. (The system can not find the specified file.)
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
I'm using SQL Server 2012.
My classes:
public class SchoolContext : DbContext
{
public SchoolContext() : base("ENTITY")
{
}
public DbSet<Student> Students { get; set; }
}
public class Student
{
public Student()
{
}
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime? DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public float Weight { get; set; }
}
Method to insert a record in the database:
protected void Button1_Click(object sender, EventArgs e)
{
using (var ctx = new SchoolContext())
{
Student stud = new Student() { StudentName = "New Student" };
ctx.Students.Add(stud);
ctx.SaveChanges();
}
}
Web.config
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
I created another solution with a Windows Forms project. I did the same thing: I installed the entity by manage nuget; I created a "student" class; a context; I also tried to insert a record by a one-button event. Now it did not give an error but it also did not create the database in SQL Server.
Personally, the problem keeps happening when it's a web application. In Windows Forms it worked. I created a new project, even adding the connectionString the problem continues, the message is the same as mentioned at the top. Is the procedure different if it's a web project?