MIgrations error loading System.Configuration.ConfigurationManager - MIgrations

1

I have a WPF project that contains a app.config containing the consection string and tb I have a Date layer of type ClassLibrary .dot net Standard on which to create the Migrations folder.

When I give an "Add-Migration Start" the system is not able to access my app.config and get the connection string to create the Migration.

This error is appearing:

  

"Could not load file or assembly   'System.Configuration.ConfigurationManager, Version = 4.0.0.0,   Culture = neutral, PublicKeyToken = cc7b13ffcd2ddd51 'or one of your   dependencies. The system can not find the file specified. "

<?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 do I do to resolve?

    
asked by anonymous 11.01.2018 / 00:37

1 answer

0

In order to create Migrations it is not enough to just start, there are some steps to follow, but you must 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.

Thereafter on the Tools menu> NugGet Package Manager > Package Manager Console:

Enable Migrations - from here you can see the Migrations folder created with two files from an Initial State and another Configuration.cs

  • Enable-Migrations if your solution has multiple projects it will be better to Enable-Migrations -ProjectName 'Nome_Projecto'

Add to Migrations - If you add another table to the database or add it to the database.

Add-Migrations NOME_MIGRATION_QUE_DESEJA -ProjectName "NOME_PROJECTO"

Migration Update - If you change some data in the database.

Update-Database -projectName "NOME_PROJECTO"

Just with what you have in the question, it is clear that there are a few steps to be taken, such as activating migrations; otherwise, the Configuration.cs file will not be created and inaccessible. I would advise you to take a look at some Code-First tutorials.

    
11.01.2018 / 16:26