Changing Database of a system that is using EF code first

Asked

Viewed 206 times

1

Please, I need to change the BD of an application using Mysql to use MS SQL Server. I have already migrated tables, data and relationships.

The problem is that the system was made, I believe, in the EF code first. What do I have to do to change the calls? Table names, primary and foreign keys and everything else is the same.

I even changed the following code:

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

That part I’m having doubts about:

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

  <defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"/>

  <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />-->
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.0.7.0, Culture=neutral, PublicKeyToken=xxxxxx" />
  </providers>

</entityFramework>

What do I need to do for EF to understand that I want it to point to MS SQL Server? Do you have cake recipe for this? Follow the full file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx" requirePermission="false"/>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>

    <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
       <defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"/>

       <providers>
          <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
          <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.0.7.0, Culture=neutral, PublicKeyToken=xxxxxx" />
       </providers>
    </entityFramework>
<!--
  <entityFramework>
    <defaultConnectionFactory type="System.Data.SqlClient.SqlClientFactory, MSSqlServer.Data"/>

    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>

  </entityFramework>
-->   
  <connectionStrings>
    <add name="ConnectionMysql" providerName="MySql.Data.MySqlClient" connectionString="server=localhost; user id=; Password=; SslMode=; database="/>
    <add name="ConnectionSqlServer" providerName="System.Data.SqlClient" connectionString="server=; user id=; Password=; database="/>
  </connectionStrings>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>

</configuration>
  • I don’t know how to answer that, but I will tell you that, contrary to popular belief and marketing of technology, this trade is not good unless you rethink much of what you’ve done or if it’s very simple things. There are some cases that were already so bad that you won’t even feel problems, and can even in extreme cases improve because everything was done to work better according to SQL Server :D

  • #Maniero, thank you for telling me, but unfortunately I have NO choice. The company wants to standardize ALL its systems on a single data storage platform.

  • Change is not the problem, the point is that it is not only change of DB and everything is beautiful as it is used to sell

  • #Maniero, exactly! I need a precise step-by-step to remove Mysql and put MS SQL Server in the same C#source code. If it were pure ADO.Net it would be laborious, but it would not get to be difficult, the problem that EF Code DOES NOT show what needs to be done.

  • I’ve never done this, but theoretically, if the project doesn’t make use of any specific SQL Server functionality, just replace the Connection string and the Provider inside the element connectionStrings, which you have already done. It has not been possible to specify in your question what happened after you made this change, whether or not you made a mistake.

  • #Vítor Martins, if it were ADO.Net PURO could be. But the "Wizard" Entity framework is not quite like that. See clauses: "<providers>", "<defaultConnectionFactory", etc...

  • Bro, if the application is code first, it creates a test base, switches the connection to that base and runs the update-database to create the structure for SQL-SERVER. With everything working, then you’ll have to migrate the data only.

  • #L. Jhon, "update-database"?!? Please gentleman, where is this interesting command?

  • I think I found something... is that it? https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx

Show 4 more comments

1 answer

1


creates a new application using sqlserver with a test table and EF etc

just to make sure you’re not forgetting some reference.

then compare web.config. add the references you will use.

preserve the connection name. exchange only the bank

in the video below it makes the update-database at 13:40

https://www.youtube.com/watch?v=sZEvfQ0D8Mg

ps. had to post here because the system does not let me comment

  • Cool Horacio Oliveira! I’ll try.

  • 1

    <Compilation debug="true" in webconfig to see the errors. if any you update there for us to see

  • Putz... I took the example of Devmedia (Entity Framework Code-First Tutorial - Devmedia), but it is very bad...

  • THIS TUTORIAL IS MUCH BETTER! Tutorial: Introduction to Entity Framework 6 Code First using MVC 5 (https://docs.microsoft.com/pt-br/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-datamodel-for-an-asp-net-mvc-application)

  • Configuration file -> https://docs.microsoft.com/pt-br/ef/fundamentals/configuring/config-file

  • Connection templates and strings -> https://docs.microsoft.com/pt-br/ef6/fundamentals/configuring/connection-strings

  • ANOTHER VERY IMPORTANT THING! If you are switching from different Bds (in the case of Mysql to MS SQL server) you need to look at the data TYPES. Example: Code First creates: VARCHAR(MAX)... switch to VARCHAR(A CERTAIN SIZE YOU NEED). Don’t use, for example: "Interger(11,0)", use "Int". In case of "Tinyint" use "Bit".

Show 2 more comments

Browser other questions tagged

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