ASP.MVC ENTITY Code First (odd problem)

Asked

Viewed 151 times

0

I do not know if I am doing something wrong (probably yes). But EF is persisting the data in a BD and in a table that it creates alone.

I’ve repeated this step-by-step twice and the result is the same.

1-I create a MVC Web Solution that I call Modelo-Index. (The idea was to practice paging and filters, but... I spent all day for nothing...)

2-Create a "sub" project within this solution called Domain and create a POCO class called Student with, Id, Enrollment, Name and Observation.

3-I create another "Sub" project and call it Persistence. I install the Entityframework on it. I add a Register classdbcontext.Cs as follows:

using Dominio;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace Persistencia
{
public class CadastroAlunosDbContext : DbContext
{
    public DbSet<Aluno> Alunos { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Aluno>().ToTable("Alunos");
        base.OnModelCreating(modelBuilder);
    }
}
}

Then I go to App.config of this Persistence project and add:

<connectionStrings>
<add name="CadastroAlunosDbContext" connectionString="Server=NB-SAUDE-03;Database=CadastroAlunos;User Id=sa;Password=Master123;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Notice that my server is called Server=NB-SAUDE-03 I also specify the database: Database=Student Register Then I run the following commands in Packagemanagerconsole:

Enable-Migrations -Projectname Persistence -Startupprojectname Persistence

Add-Migration "Base Creation" -Projectname Persistence -Startupprojectname Persistence

Update-Database -Projectname Persistence -Startupprojectname Persistence

------So far Everything works as expected---------

I go to SQL Server Objectexplorer and see that my database has been created and the Students table as well. All blz!

But when I run the application in the browser and ask to register a new student. This student did not appear persisted in the bank created. I went looking and found a bank called "Persistencia.Cadastroalunosdbcontext" that I don’t know where it came from. It also has a Students table and the data is being persisted in it and not in what I specified in the Persistence Project’s App.config. Note: This base is being created in what seems to be another instance of SQL, but I only have one installed. Why the EF is creating this bank?

inserir a descrição da imagem aqui

  • how is the config app of the main project, which makes the startup ?

  • It’s default. I’m not changing anything on it. It doesn’t even have a connectionStrings

  • 2

    The problem is that the connectionstring must be on the web.config of your MVC project so that your application knows which database to use to persist, the connection string in the app.config of your persistence project will only serve to generate the same migrations as you did. By default, when the Entity Framework does not find its connection string (in the web project) it will create the database on localDb.

  • Pts! Was that it? Pouxa life... what a disappointment... I lost all afternoon here cracking my head... I deleted the whole project 2 times. Kkkkkk! Thanks @Gabrielcoletta.

  • You are welcome. Test and share with the community if this solves the problem, if you solve I will make an answer with the solution to close the question.

  • I believe that’s all there is to it =]

  • That’s really what @Gabrielcoletta was. I added connectionStrings to the Web.Config and it went round and round. You can answer and close the question.

Show 2 more comments
No answers

Browser other questions tagged

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