EF creates database with adding guid to it's name - c#

I want to create a new database using EF6. This is my connection string. I'm using code-first.
<connectionStrings>
<add name="local"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\SampleDbName.mdf;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
After running my asp.net app EF completely creates database, but it's name changes to SAMPLEDBNAME_349a392a7d84452287b4948d4f2ab5cf (it's probably a guid).
What do I have to do to create database with name specified in connection string?

Add this to your connection string:
Database=DatabaseName
or InitialCatalog=DatabaseName
like so(if registering with dependency injection):
string connection = #"Server=server_address;Database=databaseName;PersistSecurityInfo=False;User=username;Password=pa$$;MultipleActiveResultSets=False;";
services.AddDbContext<DbContextName>(options => options.UseSqlServer(connection));
Or in your case
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\SampleDbName.mdf;Integrated Security=True;Database=DatabaseName;MultipleActiveResultSets=True" />
or if Database=DatabaseName won't work
try InitialCatalog=DatabaseName

Related

Failed to read database schema

im newbie in c# and i want fetch data from mssql local engine with peta poco orm but i get this error
image of error im getting
and here is my connection string of my class library:
<connectionStrings>
<add name="School" connectionString="Data Source= MSSQLSERVER;Initial Catalog=School;user id=sa; Password=1qaz!QAZ;" providerName="System.Data.SqlClient" />
</connectionStrings>
my database name is School, i tried server too, and my server name which i use to connect to ssms is DESKTOP-KVEJKDS
thanks for your help!
Just replace your Data Source name with your Server name:
<connectionStrings>
<add name="School" connectionString="Data Source= DESKTOP-KVEJKDS\SQLEXPRESS;Initial
Catalog=School;user id=sa; Password=1qaz!QAZ;" providerName="System.Data.SqlClient" />
</connectionStrings>

Call connection string into another connection string in web.config

I have two connections in the web.config basically they are calling the same database. I want to manage this in a better manner because change in one config also needs to change the second connection string as well.
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;persist security info=True;user id=test;password=test123;database=db-AUTH" providerName="System.Data.SqlClient" />
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=db-AUTH;persist security info=True;user id=test;password=test123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
My question is, is there any way i can call the connection string into the other connection string.
For example. default connection connection string into db entities something like
<add name="dbEntities" connectionString="metadata=res://*/InsuranceFinderModel.csdl|res://*/InsuranceFinderModel.ssdl|res://*/InsuranceFinderModel.msl;provider=System.Data.SqlClient;provider connection string= DefaultConnection" providerName="System.Data.EntityClient" />
Any suggestions would be appreciated thanks.
You are not obliged to use connection string defined inside app.config(web.config) file for entity connection. You can change entity connection string at runtime. Read this article for that: http://www.c-sharpcorner.com/UploadFile/dacca2/pass-connection-string-in-run-time-to-entity-framework/ .
Also you can get another connnection string and separete every part of connection string( DataBase,DataSource and etc.) using StringConnectionBuilder class https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.initialcatalog.aspx.
The solution is: Get DefaultConnection string and change entity connection at runtime.

web.config one connection string for both DbContext and ObjectContext

In my project, I have upgrade some of the business to DbContext, previous version was on ObjectContext.
I want to have one ConnectionString in web.config for both DbContext and ObjectContext.
<add name="OjContext" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=server-pc;Initial Catalog=db;User ID=sa;password=123;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
<add name="DbContext" connectionString="Data Source=server-pc;Initial Catalog=db;User ID=sa;password=123;" providerName="System.Data.SqlClient" />
You'll want to build your "OjContext" connection object in your code, using the "DbContext" connection string for EntityConnectionStringBuilder.ProviderConnectionString property.
See more here: https://msdn.microsoft.com/en-us/library/vstudio/Bb738533(v=VS.100).aspx

How to set a proper connection string for EF DB First?

I have an ArgumentException : Keyword not supported: 'metadata'.
I tried many things, read many posts. I dont know what to do...
This is my CS :
<add name="CDPContext"
connectionString="metadata=res://*/CDP_Model.csdl|
res://*/CDP_Model.ssdl|
res://*/CDP_Model.msl;
provider=System.Data.SqlClient
provider connection string='Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;
Integrated Security=True;
MultipleActiveResultSets=True;
App=EntityFramework'"
providerName="System.Data.SqlClient" />
Thanks.
the connection string you provided above is for model first.
to use connection string for code first you could write your connection string as below
<add name="CDPContext" connectionString="Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;integrated security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
and also go to you context, if there is a throw exception in it, just remove it.
providerName="System.Data.SqlClient" is the wrong provider for DB First
instead use providerName="System.Data.EntityClient" to access the db using the EDM...

connection string is not working in asp.net webforms

i registered my website on somee.com. for that i have uploaded MS SQL database. i was wrting this connection string in my code:
connectionString="metadata=res://*/nrcsaEntities.csdl|res://*/nrcsaEntities.ssdl|res://*/nrcsaEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=My-PC;initial catalog=nrcsa;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
now as i registered somee.com is providing me new connection string that is:
workstation id=nrcsadb.mssql.somee.com;packet size=4096;user id=DuaZoya_SQLLogin_1;pwd=abcd;data source=nrcsadb.mssql.somee.com;persist security info=False;initial catalog=nrcsadb
i have changed connectiong string in file web.config by replacing this first connection string with provided connection string by somee.com
PROBLEM:
This replacement is generating warning that:
System.ArgumentException: Keyword not supported: 'user id'.
how to solve this problem?
In the web.config file ....
<connectionStrings><add name="nameofConnection" connectionString="Data Source=servername; Initial Catalog=DatabaseName; User ID=UserName; Password=Password;"/> </connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.0" /> </system.web>
you can edit target Framework according to you.
from : http://dotnet-developers-cafe.blogspot.in/2013/08/create-connection-string-in-aspnet.html
You're using Entity Framework.
Entity Framework has its own connection string which contains a reference to the EF metadata (metadata=...) as well as the inner connection string to connect to the actual database.
You need to insert your actual database connection string inside the EF connection seting, in the provider connection string=... section.
You will also need to add multipleactiveresultsets=True to their connection string; EF needs that setting.
As you are using entity famework, then your connection string will look like
<connectionStrings>
<add name="BlogContext"
connectionString="metadata=res://*/BloggingModel.csdl|
res://*/BloggingModel.ssdl| res://*/BloggingModel.msl;
provider=System.Data.SqlClient
provider connection string="data source=[you somee.com connetion string];"" providerName="System.Data.EntityClient" />
</connectionStrings>
what you need to do is simply change the value of data source from the actual connectionstring provided by somee.com
Don't replace the whole connection string. You will need to remove the Integrated Security = true section and replace it with user=DuaZoya_SQLLogin_1;password=abcd.
Also change the data source to nrcsadb.mssql.somee.com.
You pretty much just need to replace values in your existing connection string with the values provided.

Categories