Wednesday, October 31, 2012

EntityConnection error: The specified named connection is either not found in the configuration...

While working on Entities in .Net, you may have face the following error:


EntityConnection error: The specified named connection is either not found in the configuration...


This is just a tricky error. It occurs when .Net cannot found the named connection string in your web.config or app.config file.

Example:

public NorthwindContext() : base("name=NorthwindEntities", "NorthwindEntities")
{
     // your code here
}

In this example .Net can't found "NorthwindEntities" in the configuration file.

Solution:

  1. Make sure the connection string exists in your configuration file.
  2. If you are using multi tier architecture (multiple projects in the solution) then copy the connection string from the configuration file of the project containing edmx file to the configuration file of the Windows form or Web form project (web.config or app.config) which is set as startup project.

Thursday, September 27, 2012

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid


Today I got another problem:

I added Entity Framework 4 (edmx) model in one class library project in .Net 4 and refrenced this project in another Windows Forms project to test the model. I got the following error while reading from database:

"The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid."

Solution:

Then I searched it through the internet and I found the following solution:


Assuming there are 2 projects: One that holds the EntityFramework classes (repository) and another project that is an Application project (application) that references the entity framework one. Add a reference to System.Data.Entity to the application project and copy paste the ConnectionStrings block in App.Config from the repository project into the application project:
Other option is to pass the connection string as a parameter while instantiating the classes repository:

MyPortalEntities myPortalEntities = new MyPortalEntities(connnectionString);

Tuesday, January 3, 2012

How to attach an mdf file using command prompt

If you have not already created the accounts then first you will have to create account in sql server.

Steps to Create a new account to SQL server to connect with your windows account.

1. Right click on cmd.exe (Command Prompt) and click "Run as Administrator"
2. Write :
sqlcmd -S ( is the sql server instance name. eg.: .\sqlexpress)
go
3. SQL server should now connected with administrative rights. Now you can create user account for logging in.

First create a SQL Server login for your domain account.

CREATE LOGIN [your domain account] FROM WINDOWS;
GO

your domain account has to be replaced with the domain account you want to add. For instance your domain is called darth and the user vader than it would be 


CREATE LOGIN [darth\vader] FROM WINDOWS; GO;


To check if the login was successfully created type in

SELECT NAME FROM SYS.SERVER_PRINCIPALS
GO

and the domain user should appear in the result list.

Grant sysadmin rights

You can retrieve a list of server roles using the sp_helpsrvrole procedure. So to add the user to the sysadmin server role group type the following (example is still domain darth user vader)

SP_ADDSRVROLEMEMBER ‘darth\vader’, ‘sysadmin’
GO

Done.
Quit SQLCMD and command prompt and open again as domain user instance with your domain account. Type in select user_name() and the result should be dbo.


To Attach a Database File


1.Open the command prompt on the server.



2.From the command prompt, connect to an instance of SQL Server by using the following sqlcmd command:



sqlcmd -S Server\Instance

Where Server is the name of the computer and Instance is the name of the instance.



3.When connected, type the following commands:


USE [master]

GO

CREATE DATABASE [database_name] ON

( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\.mdf' ),

( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\.ldf' )

FOR ATTACH ;

GO

Where database_name is the name of the database you want to attach, FileName is the path and filename of the database file and the log file, and FOR ATTACH specifies that the database is created by attaching an existing set of operating system files.


4.To verify that the database has been attached, type the following two commands:

select name from sys.databases
go

5.The sqlcmd tool displays the names of all databases attached to this instance of SQL Server Express. In the list, you should see the database name you provided in step 3.


Popular Posts