Change connection string in EF from localDB to SQL Server instance - c#

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.

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.

Migrate ASP.NET MVC from SQL Server to Oracle

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?

How to solve Server Error in '/' Application Error?

I am learning Entity Framework and trying to create a sample code using Code first approach. While running this code i am getting above mention error. I am developing code on VS 2012 and database is Microsoft SQL Server. it also shows error message as "Unable to find the requested .Net Framework Data Provider. It may not be installed."
I've developed 3 class files and one of this file is for interacting with database which throws error at:
public List<Standard> GetStandards()
{
StandardDBContext standardDBContext = new StandardDBContext();
return standardDBContext.Standard.Include("Student").ToList();
}
My web.config file:
<connectionStrings>
<add name="StandardDBContext" connectionString="server=CPC2\SQLEXPRESS; database=SchoolDatabase;User Id=sa;Password=password; integrated security=true" providerName="System.Data.SqlClient;" />
</connectionStrings>
<system.data>
<DbProviderFactories>
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</DbProviderFactories>
</system.data>
<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" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
Can any one please help me. Your help will be appreciated.

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