Change server Entity Framework - asp.net-mvc

I'm working on an mvc .net application and I'm using Entity Framework. I have the same database in different servers and I want to change the current server. I tried to change the connection string in the web.config file but it didn't work. How to safely change the server?

You must replace Data Source
<connectionStrings>
<add
name="AdventureWorksEntities"
connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;
provider connection string='Data Source=.....;
Initial Catalog=....;
Integrated Security=True;
Connection Timeout=60;
multipleactiveresultsets=true'"
providerName="System.Data.EntityClient" />
</connectionStrings>

Changing the connection string in web.config is all you need to do.
Make sure you are changing the right connection string (the one with the metadata). Entity Framework will automatically put a connection string in the web.config when the model is first created.
The correct connection string will look something like this:
<add name="AdventureWorksEntities"
connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />

Related

Store user name and password with neo4j connection string

What is the format (if any) for adding user name and password to neo4j v4 connection strings? So far, I've seen just examples like bolt://localhost:7010 with authentication being added as extra arguments to the connection calls.
Here's why I'm asking:
I have an ASP.NET app which connects to both MS SQL and neo4j DBs. For MS SQL, I can add the connection string to the /configuration/connectionStrings in web.config formatted like this
<add name="MyDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDB;UID=user;PWD=pwd" />
or
<add name="MyDB" connectionString="Data Source=.,1433;Initial Catalog=MyDB;UID=user;PWD=pwd" />
It contains host name, instance name or port name, plus the credentials. I can then encrypt the connectionStrings part of web.config using standard IIS tools.
For ease of management, I'd like to reuse it for neo4j connection strings as well. I could store the string in MSSQL-like format
<add name="MyNeo4jDB" connectionString="Data Source=.,7010;UID=user;PWD=pwd" />
but I don't want to invent my own format.
Thanks

my website doesn't retrieve data from database

I have an ASP.NET MVC web application that communicate with sql server with entity framework.
Now i want to upload my website in a server. I've created database
in that server and restore my local database backup in it then i published my application in filesystem and upload it in server.
My problem is connection string in web.configi know that in connection string we should define data source , initial catalog , user id and password and my connection string is like this :
<add name="Bestshooter1Entities" connectionString="metadata=res://*/Models.DomainModel.Model.csdl|res://*/Models.DomainModel.Model.ssdl|res://*/Models.DomainModel.Model.msl;connection string="Data Source=./MSSQLSERVER2014;Initial Catalog=bestshoo_Database;User Id=bestshoo_DatabaseAdmin;Password=******;Persist Security Info=True;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" />
But it still does not work, what is wrong?
The password of your connection string is ***** please replace it with original password.
Some servers allow you to deploy your database on server and provide connection string to use in your application.
Can you please try with Provider Name:
<connectionStrings>
<add name="MyDbContext" connectionString="data source=.\sqlexpress;initial catalog=YourDbName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
</connectionStrings>
You just add ProviderName to tag <add />.
<add name="MyDbContext" connectionString="data source=.\sqlexpress;initial catalog=YourDbName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>

How to create multiple edmx in same project

i am new to EF ...
getting this error while adding another edmx in same project
using same connection string .
this is my connection string
<add name="REFLECTIONDBEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="data source=SERVER;initial catalog=REFLECTIONDB;persist security info=True;user id=sa;password=Sqlsa123;multipleactiveresultsets=True;application name=EntityFramework"" />
Same connection Strings Names are not allowed, you can have multiple Connection Strings but with separate names.
Change
name="REFLECTIONDBEntities"
To
name="REFLECTIONDBEntities2"

Splitting an Solution into multiple projects

I created a Black Solution (Arhi) with a ASP.NET MVC 4 Internet project (Arhi.Core) and Class library project for data (Arhi.Data) where I'm storing my EDMX Model.
I added a reference for Arhi.Data into Arhi.Core and I tried to add a Controller with a Model class from Arhi.Data (People entity) and I got this error.
'Unable to retrive metadata for 'Arhi.Core.People'. The specified
named connection is either not found in the configuration, not
inteneded to be used with the EntityClient provider, or not valid.'
Q : Why did I get this error ? Is my approach wrong / any recommendations?
Q2 : If I want to add RDLC reports to my solution, should I also use a Class Library project ?
Connection string from Arhi.Core
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-SalvamontMVC-20121108140556;Integrated Security=SSPI" />
and Arhi.Data
<add name="SalvamontEntities" connectionString="metadata=res://*/ModelSalva.csdl|res://*/ModelSalva.ssdl|res://*/ModelSalva.msl;provider=System.Data.SqlClient;provider connection string="data source=www.arhimedes.ro,1433;initial catalog=Salvamont;persist security info=True;user id=sa;password=********;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
in your first connection string not have user name and password if two database deference then give user name and password in first connection string

MVC Entity Framework connection string references other project

I'm doing a Model first approach for a Microsoft MVC application. The solution is named "TutorialPile" divided into two projects, Domain and WebUI. I try to add a controller for the Tutorial object to the WebUI project, and I select the domain class and the DB context. However, I get the error, "Unable to retrieve metadata for TutorialPile.Tutorial. Unable to load the specified metadata resource."
Looking around online it looks like it can't find the edmx object in the connection string in the web.config file. I copied the connection string from the Domain project's app.config file but it still doesn't work. Here are the connection strings from the web.config file.
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="TutorialPileModelContainer" connectionString="metadata=res://*/Models.TutorialPileModel.csdl|res://*/Models.TutorialPileModel.ssdl|res://*/Models.TutorialPileModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=TutorialPileDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="TutorialPileDbContext" connectionString="metadata=res://*/Models.TutorialPileModel.csdl|res://*/Models.TutorialPileModel.ssdl|res://*/Models.TutorialPileModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=TutorialPileDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
And here is the connection string that I copied.
<add name="TutorialPileDBEntities" connectionString="metadata=res://*/TutorialPile.csdl|res://*/TutorialPile.ssdl|res://*/TutorialPile.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sqlexpress;initial catalog=TutorialPileDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Any ideas on what I need to change?
Make sure...
the edmx file's build action is set to EntityDeploy (select the file and go to the properties window).
Your WebUI project references the Domain project
Edit the connection string on your WebUI project to reference the metadata from the other project. Using * means it can come from any dll. BUT, if your edmx file is inside a folder, you have to map the hierarchy.
Eg:
Path to EDMX: TutorialPile.Domain/Model/TutorialPile.edmx
Connection string: res://*/Model.TutorialPile.csdl|res://*/Model.TutorialPile.ssdl|res://*/Model.TutorialPile.msl
Even better:
The documentation suggests you specify the assembly (for performance reasons) using the full name of the assembly (something like: AdventureWorks, 1.0.0.0, neutral, a14f3033def15840). I couldn't make it work. But, using only the name of the assembly works for me. So, if your domain project outputs a TutotialPile.Domain.dll, you can use:
res://TutorialPile.Domain/Model.TutorialPile.csdl|res://TutorialPile.Domain/Model.TutorialPile.ssdl|res://TutorialPile.Domain/Model.TutorialPile.msl

Resources