Entity Framework - Error?

2

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?

    
asked by anonymous 09.06.2017 / 23:51

1 answer

1

There were two problems, the message in my question that was resolved after I removed the constructor from the context class:

public class SchoolContext : DbContext
{
    public SchoolContext() : base("ENTITY")
    {
    }
    public DbSet<Student> Students { get; set; }
}

The other problem of not creating the tables was solved by adding a connectionString in Web.config or App.config.

My App.config looks like this:

<configuration>
  <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>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <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>

  <connectionStrings>
    <add name="EntityContext" connectionString="Server=MeuServer; Database=teste; uid=sa; password=asda;" providerName="System.Data.SqlClient" />
  </connectionStrings>

</configuration>

So my question is: is it mandatory to put the connectionString or not?

    
10.06.2017 / 02:21