Entity Framework bank creation

Asked

Viewed 42 times

0

I’m new to Entity and I’m following a step by step to create a Code First application ( http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx ).

I created the application, debugged and it runs perfectly without any error, but no database is created... Follow the Main code and App.config:

I think it’s some configuration problem or something... Someone more experienced has an idea of what it might be?

static void Main(string[] args)
    {
        using (var ctx = new SchoolContext())
        {
            Student stud = new Student() { StudentName = "New Student" };

            try
            {
                ctx.Students.Add(stud);
                ctx.SaveChanges();
            }catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }


    <?xml version="1.0" encoding="utf-8"?>
    <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>
      <connectionStrings>
        <add name="entityFramework" connectionString="Server=localhost\SQLEXPRESS;Database=master;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
    </connectionStrings>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
      </startup>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
         <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

1 answer

0

When I deal with Entity, I create the modes, define the context class (with Dbset), go to the VS console and do the following: (this after installing Entity)

Enable-Migrations

Add-Migration Create bank

Update-database -verbose

In this he verifies the existence of the database and compares the tables with the models, editing the database according to changes made in the model. There is no bank, it creates.

I can’t tell if that helps

  • Thanks for the reply friend. I made the configuration but the database is not yet created when running...

Browser other questions tagged

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