Entity Framework - Error?

Asked

Viewed 309 times

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?

  • 2

    Did you miss telling Connectionstrings? I guess that would be it.

  • There is no such ENTITY.mdf file but the strange thing is that even if I create another project the error persists.

  • Isn’t Connectionstrings when you already have the database created in sql server? In the tutorial you didn’t mention anything about Connection string.

  • When you run directly in Visual Studio you also have @Heyjoe, example: "Server=(localdb) Mssqllocaldb;Integrated Security=true" and tutorial

  • Another link to reading

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

  • 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 ...!

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

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

Show 5 more comments

1 answer

1


There were two problems, the one of the message in my question that was solved 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 to Web.config or App.config.

My App.config got 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 connectionString or not?

  • Actually the first Connectionstring should already work (the one in the question). Have you enabled Migrations? How is its configuration?

  • In the other case I had enabled Migrations, but in this new project I did not do this, I only removed some parts leaving them as comments, as you can see in my App.config, and added the connectionString. In my question does not have a connectionString, has a defaultConnectionFactory.

  • There was no connectionString in the booklet, it drove me crazy. I saw this in a video on youtube, which still showed that the name of connectionString has to be the same as the context, if it were not for that video I would be until now without knowing what to do and is such a simple information.

  • Yeah, really, I wasn’t paying attention. connectionString is required yes, by default webconfig usually creates one with the name "Defaultconnection".

  • The problem continues when I create a web application. If it is windows Forms it works. The procedure is different when it is web?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.