I have code using EF with a connection string as in web.config (spaces and line breaks for clarity) :
connectionString=
metadata=res://*/Models.XXXDataContext.csdl|
res://*/Models.XXXDataContext.ssdl|
res://*/Models.XXXDataContext.msl;
provider=System.Data.SqlClient;
provider connection string="data source=xxxxxxxxxx.database.windows.net,1433;
initial catalog=db1;
user id=dba;
password=its-a-secret(bazinga!);
MultipleActiveResultSets=True;
App=EntityFramework"
I want to use the same connection string to execute a stored procedure to perform a merge insert/update to another table.
Can I user the connection string as is to create sqlConnection(cnns-tring)?
What is the purpose of the metadata=res: ... entry here? also MultipleActiveResultSets=True and App=EntityFramework? Would they have any impact here?
The metadata property specifically points to the location of the .SSDL (Storage Model,) .CSDL (Conceptual Model,) and .MSL (Mapping Model) files. These three files essentially are the Entity Data Model. The "res://" URI-style qualifier indicates that the files are embedded as resources in the compiled EDM assembly.
MultipleActiveResultSets - Multiple Active Result Sets (MARS) is a feature that works with SQL Server to allow the execution of multiple batches on a single connection. When MARS is enabled for use with SQL Server, each command object used adds a session to the connection.
App=EntityFramework: It's just the synonym of the Application Name.
All connectionstring keywords are explained here: https://msdn.microsoft.com/en-gb/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
Related
I have a azure V1 function using a project dll that handles entity framework.
First I set connect string like
metadata=res://*/Dev.csdl|res://*/Dev.ssdl|res://*/Dev.msl;
provider=System.Data.SqlClient;
provider connection string='
data source={IP};initial catalog={DBName};
persist security info=True;
user id={User};password={PW};
MultipleActiveResultSets=True;
App=EntityFramework'
and I got
Keyword not supported: 'metadata'.
then I changed my connect string to
data source={IP};initial catalog={DBName};persist security info=True;user id={User};password={PW};
and I got
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
And here's my code
DevEntities db = new DevEntities();
var lstAcAccount = db.AcAccounts.ToList();
return req.CreateResponse(HttpStatusCode.OK, lstAcAccount);
DevEntities is from other dll project that using the connect string above.
So, what should I do to make this work?
You shouldn't use generated connection string, now you have all metadata files included in your solution. Instead try use in connection string section of app.config:
"data source=localhost\sqlexpress; initial catalog=sample; integrated security=True;MultipleActiveResultSets=True;"
If it is Database first:
Open the .edmx[Diagram] -> right click -> "Update Model from database"
And see if the will appear the "Add", "Refresh" and "Delete" tabs.
If doesn't... probably your connection is broken and the dialog for VS creates a new connection string will appear instead.
Hope it helps.
I'm building an MVC web application with a small team and am working on code that will allow each of us to have a local test database on our own machine using an EDM as the source framework. I created the model, then each of us will have to "Generate DB from Model" and do the initial database set up for each of our machines.
The roadblock I hit is I can't figure out how to pull the section of the huge Metadata connection string that gets automatically generated and stored in Web.config I need to set up the SQLConnection for our queries
Basically I want to take this massive string that gets returned by ConfigurationManager.ConnectionStrings[dbName].ConnectionString;:
connectionString="metadata=res://*/Models.TestDataModels.LocalTestData.csdl|res://*/Models.TestDataModels.LocalTestData.ssdl|res://*/Models.TestDataModels.LocalTestData.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-SHUF4P9\SQLEXPRESS;initial catalog=myLocalTestDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
and access only this portion of it:
connection string="data source=DESKTOP-SHUF4P9\SQLEXPRESS;initial catalog=myLocalTestDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"e;
Normally I would just hard-code it into a Data.config file, but since this is a Local test DB, the connection string is going to be different for each of us.
I have an ASP.NET MVC2 application running with SqlServer. I need to support multiple databases (SqlServer, Mysql and Oracle). The schema is the same for all databases.
The edmx is configured to run with SqlServer, so I added two new ssdl files: one for MYSQL and one for Oracle.
With SQLSERVER I can run the application, but when I try to set the connection string and metadata at runtime to be configured with MYSQL it generates exception:
How would I go about altering the ssdl to achieve that?
I don't know how to do it specifically for MySql but this is what I've done to achieve multiple database support for Sql Server and Oracle. I believe the same approach works for MySql also. I expect you know how to set up Oracle client configuration. Changing the active connection string in application configuration file is all I need to do to target a different db.
1) Generate edmx against Sql Server.
2) Generate .ssdl and .msl files for Oracle. The latter contains entity property mappings. Data types need to be changed in ssdl for Oracle. See this link for detailed information. Oracle ssdl file needs to have the correct provider configuration, in my case: Provider="Oracle.ManagedDataAccess.Client".
3) Create new connection string for Oracle db connection in your application configuration file. It is crucial that namespaces are typed correctly here. Below is a working example:
<add name="MyConnectionString"
connectionString="metadata=res://*/CustomDb.csdl|res://CustomDbProject/CustomDbNamespace.CustomDb.ssdl|res://CustomDbProject/CustomDbNamespace.CustomDb.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=MYDS;USER ID=****;Password=****;Max Pool Size=**;Connection Timeout=120""
providerName="System.Data.EntityClient"/>
4) Create a default constructor for context:
public CustomDb()
: base("name=MyConnectionString")
{
Configuration.ProxyCreationEnabled = false;
}
I deployed C# application and created installer. Installer gets created successfully but when tried to launch the application it throws up following error:
An attempt to attach an auto-named database for file CampusPointe.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
connection string in app.config is as follows:
<connectionStrings>
<add name="App_Key_Management.Properties.Settings.VendorKeyConnectionString1"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Program Files\Vendor Key Management\VendorKey.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Any help would be appreciated.
Regards,
Sri
Sri,
First of all, you need to ensure a couple of things:
1. You have installed SQL Server
2. You need to deal with the creation or replacement of the file you created. I think this is your issue ... If the .mdf file exists then you need to either update the existing file or drop and replace it (USE [database] GO DROP TABLE... GO CRATE TABLE ... GO). You will also need to consider step 4 below.
3. If you are accessing a a file located in a database you need a fully formed name. You also need to add the database user to the connection string in ASP.NET Web.Config or App.config (depending on the app). I looks like you have started this ...
4. Make sure the user account that you are using in the connection string has the necessary privileges on the database side. If this is a full fledged database in SQL SERVER then the user account will need update, delete, create privileges. If you are replacing or creating databases on the fly then the user will need CREATE DATABASE or DROP / CREATE privileges.
I think to best answer your question we would need more information on the architecture of the application. My guess is that your code creates the database fine the first time and then when you try to run it the second time it fails because the database is already there. If this is the case you also need to address your code (with IF statements or SWITCH CASE ... your call) to handle situations where the table (1) does not exist and needs to be created; (2) exists and needs to be updated; and / or (3) exists and needs to be replaced. The correct answer again depends on your code and what you are trying to do.
I have a SQL Server 2008 database that uses a default schema called "Arxame". How do I specify or change the default schema from "dbo" to "Arxame" in my connection string? I'm using C# and ADO.NET.
You can't do that. You have to set the schema "Arxame" to the user you have specified on your connection string. You can do this using the SQL Server Management tool
If you need to change the default schema for an existing user you can do it like this
B. Changing the default schema of a user
The following example changes the default schema of the user Mary51 to Purchasing.
USE AdventureWorks2008R2;
ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;
GO
Source: MSDN
The InitialCatalog is indeed the database name. The schema that is used would depend on the user you specify, since schemas typically map to database users. Whatever user owns the Arxame schema is the one you should specify in the connection string.
I do not believe you can do this within a connection string, nor should you be able to. You use schemas, in much the same was as a namespace in C#, to further resolve securable objects within a database when there may be name collisions.
Schemas in SQL Server 2005 and up
The initial schema must be set in your connection string:
Data Source=localhost;Initial Catalog=Arxame;Integrated Security=True
Remember to use Integrated security only if you are using a Local Sql Server.