Migrate ASP.NET MVC from SQL Server to Oracle - c#

I am developing a web application using ASP.NET MVC with Entity Framework. After I finished, the IT told me that they cannot use SQL Server localdb on their server, so I need to transfer to an Oracle database. The problem is, I have no idea how to accomplish this.
What I managed to far: I found out I need to use Oracle Entity Framework and somehow managed to add Oracle.ManagedDataAccess.EntityFramework (the internet is pretty restricted here and I could not use NuGet). I also modified the connection string and provider. The problem is, an exception occurs:
An exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in EntityFramework.dll but was not handled in user code
Additional information: An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file.
Web.config:
<add name="DBCS"
connectionString="DATA SOURCE=source;PASSWORD=pass;PERSIST SECURITY INFO=True;USER ID=user"
providerName="Oracle.ManagedDataAccess.Client" />
...
<entityFramework>
<defaultConnectionFactory
type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory, Oracle.ManagedDataAccess.EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client"
type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</providers>
</entityFramework>
Context and how I use it:
public class InventoryContext : DbContext
{
public InventoryContext() : base("DBCS")
{ }
...
}
InventoryContext db = new InventoryContext();
db.Items.ToList() //use the rows
How can I make it work?

Related

Login failed for user during update-database

I have a big problem. I tried to use another solution but didn't help me.
I try to make initial migration (code-first workflow) to database which is on my computer.
I used commands in PM like enable-migrations, then add-migration NameMigration, then update-database and at this moment I have an error like:
Error Number:4060,State:1,Class:11
Cannot open database "CodeFirstDemo.Program+BlogDbContext" requested by the login. The login failed.
Login failed for user 'SZYMON\Szymon'.
My config:
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=SZYMON\BASE; Initial Catalog=CodeFirstDemo; Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
Database is not exist yet. My object explorer in MS SQL Server looks
enter image description here
Server properties looks
enter image description here
I don't know what is wrong. Please help me to solve problem.
It does not seem that you have access to the database server. Try to login using SQL Management Studio to SZYMON\BASE db server using windows authentication.

MySQL and C# Entity Framework "ProviderIncompatibleException"

i'm following the MySql tutorial to entity framework as listed here: https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html
I have installed in my project via NuGet:
- Entity Framework v6.2.0
- MySql.Data.Entity v6.10.7
And their corresponding dependencies.
I have installed the MySql CONNECTOR/NET v8.0.11.
I added the provider and connectionString to the App.config:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<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.10.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</providers>
</entityFramework>
<connectionStrings>
<add name="mysql" connectionString="Server=localhost,3306;Database=eneagramas;Uid=root;Pwd=1234;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Basically i followed all the instructions in the official documentation, this is my context class:
class MyContext : DbContext
{
public MyContext() : base("mysql")
{
//nothing here
}
public DbSet<Humans> Humans { get; set; }
}
YET i get this error when accesing the DB:
System.Data.Entity.Core.ProviderIncompatibleException
HResult=0x80131501
Message=The provider did not return a ProviderManifestToken string.
Source=EntityFramework
StackTrace:[...]
Inner Exception 1:
MethodAccessException: Attempt by method 'MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(System.Data.Common.DbConnection)' to access method 'MySql.Data.MySqlClient.MySqlConnection.get_Settings()' failed.
I checked my DB credentials, they are fine
The server is up and running
The database im trying to use exists
You can't use MySql.Data 8.0.11 with MySql.Data.Entity 6.10.7; the major versions are incompatible.
Oracle renamed the package to MySql.Data.EntityFramework for v8. Uninstall MySql.Data.Entity and install MySql.Data.EntityFramework instead.

Change connection string in EF from localDB to SQL Server instance

I don't want to use localdb when debugging my ASP.NET MVC project. When I used the database-first wizard to create the model, it correctly connected to my SQL Server instance. However, any time I run my project locally (F5), it's connecting to the localdb.
How do I use my SQL Server instance instead of localdb when running my project in debug locally?
I have tried the suggestions here.
web.config:
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-hpmvc-20151014093518.mdf;Initial Catalog=aspnet-hpmvc-20151014093518;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<add name="MyConnectionStringName"
connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=MyServer\MyNamedInstance;initial catalog=MyDatabase;user id=MyUser;password=MyPassword;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
...
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="MyConnectionStringName" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
And DbContext:
public partial class MyEntities : DbContext
{
public MyEntities() : base("MyConnectionStringName")
{
...
}
}
And yet, when I run debug on the Index view, I'm seeing the test records in localdb and not the records in my SQL Server instance.

SQL Server Compact 4.0 connection string for Entity Framework code first?

I am creating an application for Windows XP so I am stuck with .Net framework 4.0. I tried to use SQL Server Compact and EF code first but it make an error when update-database.
I wish to put the database in my code directory to deploy in customer machine.
This is my connection string:
<add name="QuanLyKhoContext"
connectionString="Data Source=MyData.sdf;Persist Security Info=False;AttachDBFileName=MyData.sdf"
providerName="System.Data.SqlClient" />
And the error:
This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.
In one of my projects I used SQL Server Compact 4.0 and my connection string was simple enough:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=|DataDirectory|MyDB.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
I think that you should check your providerName. I am not sure if System.Data.SqlClient is the right one.
Have you installed EntityFramework.SqlServerCompact NuGet package? Once it is installed System.Data.SqlServerCe.4.0 provider name is added and it should be used in the connection string.
Check if SqlServerCompact provider is added to your web.config.
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
To prevent this you need to change your connection string to have:
Trusted_Connection=False;Persist Security Info=True
based on below link
http://jayhollingum.blogspot.in/2011/03/ef-codefirst-databasesetinitializser.html
After many search, I decide to move to a near approach of SQL compact, it's LocalDB. I change it and this work:
<add name="QuanLyKhoContext" connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=60" providerName="System.Data.SqlClient" />
I think "System.Data.SqlClient" is a valid Provider because I am using Sql Express LocalDB :)
It's not my first purpose, but it work anyway. Thanks all
I just don't know when deploy in window XP, do I need to install Sql Express or Sql Compact stuffs manually?
According to MSDN documentation you can use the following:
<add name="ConnectionStringName"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=|DataDirectory|\DatabaseFileName.sdf" />
PS: How to install SQL Server Compact

EF 6 seamless connection with both Ms SQL and MySQL

I have ASP.NET MVC application with default MS SQL 2012 data source. I need to make same app to work with MySQL db without much code change or separate compilation since both database are exact replica. I have browsed and notice following solutions :
Create separate SSDL using edmgen tool. - couldn't complete implementation example
Create separate EDMX - but how can I just switch between MsSQL and MySQL in web.config file
Applying [DbConfigurationType(typeof(MySqlEFConfiguration))] in DAL module - but this require separate compilation.
I referred following in my project :
MySQL.Data
MySQL.Data.Entity.EF6
And following is the settings in my web.config file :
<connectionStrings>
<add name="DefaultConnection" connectionString="server=localhost;user id=root;password=xxxxx;persistsecurityinfo=True;database=pscope4" providerName="MySql.Data.MySqlClient" />
<add name="DefaultConnection-SQL" connectionString="Data Source=.\SQLSERVER2012;Initial Catalog=PScope4;persist security info=True;user id=sa;password=xxxxx" providerName="System.Data.SqlClient" />
<add name="PScopeDB" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=xxxxx;persistsecurityinfo=True;database=pscope4"" providerName="System.Data.EntityClient" />
<add name="PScopeDB-SQL" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLSERVER2012;initial catalog=PScope4;persist security info=True;user id=sa;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
I'm using Asp.Net.Identity, and its working fine with both MsSQL and MySQL. But am getting following error in my EF :
Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'System.Data.SqlClient.SqlConnection'.

Categories