2
I’m following a tutorial from Entity framework
: Simple Code First Example it is quite simple, but for some reason it is not working here and appears the error message is as follows:
Aspnet Projects ENTITY ENTITY App_data ENTITY.mdf" failed with the Operating system error 2(The system cannot find the specified file.).
CREATE DATABASE failed. Some file Names Listed could not be created. Check Related errors.
I am 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 enter 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 Entity by Manage nuget; I created a "student" class; a context; I also tried to insert a record by a button event. Now no error but did not create the database in SQL Server.
Guys, 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 mentioned at the top. The procedure is different if it is a web project?
You have already checked in the App_data folder whether the ENTITY.mdf file exists?
– Tiago S
Did you miss telling Connectionstrings? I guess that would be it.
– novic
There is no such ENTITY.mdf file but the strange thing is that even if I create another project the error persists.
– HeyJoe
Isn’t Connectionstrings when you already have the database created in sql server? In the tutorial you didn’t mention anything about Connection string.
– HeyJoe
When you run directly in Visual Studio you also have @Heyjoe, example: "Server=(localdb) Mssqllocaldb;Integrated Security=true" and tutorial
– novic
Another link to reading
– novic
I see two tutorials, both say that the Entity that creates the bank, so much so that in the context was put the name that is to be the bank in the constructor.
– HeyJoe
You can create yes, but you are learning wrong ... it was only two links to tips! Correct is installing the Sqlserver server on the machine and running Migration ... but, quiet ...!
– novic
I am using SQL Server 2012. Both Code First tutorials do not say to create the database first, the context that will do this. But it didn’t work here. I also tried Enable-Migrations and didn’t create the database. I will try to create another.
– HeyJoe
I could not create the database by code first, had to create in SQL Server and add a connectionString in the application. The context constructor that was used to create the database by code first I had to remove.
– HeyJoe