Estou seguindo um tutorial de Entity framework
: Simple Code First Example ele é bem simples, mas por alguma razão não está funcionando aqui e aparece a mensagem de erro é a seguinte:
AspNet\Projetos\ENTITY\ENTITY\App_Data\ENTITY.mdf" failed with the operating system error 2(O sistema não pode encontrar o arquivo especificado.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Estou usando o SQL Server 2012.
Minhas 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; }
}
Método para inserir um registro no banco:
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>
Criei outra solution com um projeto Windows Forms. Fiz a mesma coisa: instalei o entity pelo manage nuget; criei uma classe "student"; um context; também tentei inserir um registro por um evento de um botão. Agora não deu erro mas também não criou o banco no SQL Server.
Pessoal, o problema continua acontecendo quando é uma aplicação web. No Windows Forms deu certo. Eu criei um novo projeto, mesmo adicionando a connectionString o problema continua, a mensagem é a mesma mencionada no topo. O procedimento é diferente se for um projeto web?