My name is Archana CM from Microsoft SQL Developer Support team, we support many data access technologies including Entity Framework, SSIS.
I had chance to work with developer who was having issues in his Entity Framework, one of the issue was while adding data to .mdf file which was on file system.
In today's blog I am sharing my experience on how we could resolve the issue for him and what issues he was facing.
It was Windows application and Entity Framework was used. As a backend SQLExpress was used in his application and he was saving data to .mdf file which was on file system.
When we executed the application and while trying to add data to .mdf file we could see below error message
Message: The underlying provider failed on Open.
Stack trace : at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
at System.Data.EntityClient.EntityConnection.Open()
at System.Data.Objects.ObjectContext.EnsureConnection()
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
at EFLenoard.DataMgr.AddFacility(String name, String address, String city) in D:\Research\EFParentChildInsert\EFParentChildInsert2010\DataMgr.cs:line 35
Inner Exception : InnerException = {"An attempt to attach an auto-named database for file D:\\Research\\EFParentChildInsert\\EFParentChildInsert2010\\bin\\Debug\\SplDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC shared …
Here is the connection string that was used, while issue was occurring.
<connectionStrings>
<add name="SQLDBEntities" connectionString="metadata=res://*/SplDBModel.csdl|res://*/SplDBModel.ssdl|res://*/SplDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\SQLDB.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
In this case, Windows authentication with user instance was used to connect to SQL server which was mainly causing the issue.
In order to resolve the above issue, we had 2 solutions
Solution 1:
In the existing connection string to remove the “user Instance=true” and it works.
Probable cause of the issue could be as below:
Solution 2:
We created new connection to database with SQL Authentication as a workaround.
Thus connection string turns out to be as below.
<add name="SQLDBEntities" connectionString="metadata=res://*/SplDBModel.csdl|res://*/SplDBModel.ssdl|res://*/SplDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=D:\SQLDB.mdf;persist security info=True;
user id=saa;password=***;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Once applying either of these solutions, with below code we could save data to .mdf file which was on file system. By attached the .mdf file to SQL Server data can be confirmed.
public class DatatoMDFOnFileSystem
{
private SQLDBEntities _sqlDataContext = new SQLDBEntities();
private string _errorMessage;
public long AddDatatoMDFOnFileSystem(string name, string address, string city)
_errorMessage = String.Empty;
try
string connectString = ConfigurationManager.ConnectionStrings["SQLDBEntities"].ToString();
using (_sqlDataContext = new SQLDBEntities(connectString))
EFDatatoMDFOnFileSystem efDatatoMDFOnFileSystem = EFDatatoMDFOnFileSystem.CreateEFDatatoMDFOnFileSystem(0, name, address, city);
if (efDatatoMDFOnFileSystem != null)
_sqlDataContext.AddToEFDatatoMDFOnFileSystem(efDatatoMDFOnFileSystem);
_sqlDataContext.SaveChanges();
id = efDatatoMDFOnFileSystem.Id;
Console.WriteLine(String.Format("Record added with id, {0}.", id));
}
catch (Exception err)
_errorMessage = err.Message;
finally
Console.WriteLine("Error Message, {0}.", _errorMessage);
return id;
Happy Coding!!!!
Author : Archana , SQL Developer Engineer , Microsoft
Reviewed by : Snehadeep(MSFT), SQL Developer Technical Lead, Microsoft
I am having different issue. In a load balanced environment on node A app is working fine but on node B I am getting following error.The underlying provider failed on Open.
Exception: System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
Source: System.Data.Entity
Please help.
This looks like a delegation issue in a double hop scenario.
though not sure but I guess this is your architecture-:
client===>IIS===>SQL
And probably your IIS is load balanced here. If that is the case follow the following steps.
1) Domain users, who is logging into the client and running the browser, must not have their domain user accounts marked as "Account is sensitive and cannot be delegated"
2) The service account under which the Middle tire SQL instance is running or the iis app pool is running must be “Trust this user for delegation”, configured in Active Directory.
3) If the middle tire service(app pool) is running as “NT AUTHORITY\SYSTEM” or “NT AUTHORITY\NETWORK SERVICE”, the Middle Tire computer must have “trust computer for delegation” checked in Active Directory.
4) Middle tier service account (app pool account) should be in the following groups.
i. Act as part of operating system.
ii. Impersonate a client after authentication.
Well I faced the same problem.
Open the command prompt as administrator and type the following command -
netsh Winsock reset
It worked for me.
It did not for me.