Migrations error loading System.Configuration.Configurationmanager - Migrations

Asked

Viewed 260 times

1

I have a WPF project that contains a app.config containing the consection string and tb have a Data layer of type Classlibrary . dot net Standard in which the Migrations folder should be created.

When I give an "Add-Migration Start" the system is unable to access My app.config and take the Connection string to create Migration.

This error is appearing:

"Unable to load file or Assembly 'System.Configuration.Configurationmanager, Version=4.0.0.0, Culture=neutral, Publickeytoken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the specified file."

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
<connectionStrings>
    <add name="SistemaComercial" connectionString="Data Source=.\sqlexpress;Initial Catalog=SistemaComercial;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient" />    
  </connectionStrings>
</configuration>

What I do to solve?

  • show how it creates the Migrations on the console.

  • Add-Migration Start

  • NA vdd, I can’t create any. Gives the error I showed above.

  • check the answer I left below

  • check which Packages.config of your application has System.Configuration.Configurationmanager and see if you have it in the references, if I haven’t been using just delete the line that has it and compile the project.

1 answer

0

To create Migrationsnot only do "start", there are some steps you should follow, before you have to contain in the project the code needed to work with Migrations:

 public class o_nome_do_seu_modeloContext : DbContext
        {
            public DbSet<O_SEU_MODELO> nome_variavel { get; set; }
        }

And then in the main program, access this information from the database, instantiating the variable for the class you created earlier.

After that in the menu Tools>Nugget Package Manager > Package Manager Console:

Activates the Migrations - from here you can see the Migrations folder created with two files from one Start State and another Configuration.cs

  • Enable-Migrationsif your solution has several projects will be better Enable-Migrations -ProjectName 'Nome_Projecto'

Add to Migrations - If you add another table to the database or for insurance add it in the same.

Add-Migrations NOME_MIGRATION_QUE_DESEJA -ProjectName "NOME_PROJECTO"

Migration update - If you change any data in the database.

Update-Database -projectName "NOME_PROJECTO"

Just with what you have in the question, it is clear that there are some steps missing, like activating Migrations, otherwise you have this, the file Configuration.cswill not be created and inaccessible. I advise you to take a look at some tutorials of Code-First.

  • Enable-Migrations No EF-Core ??? still has this ???

Browser other questions tagged

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