I'm working in Visual Studio 2012 and trying to connect my website to an mdf database file (sisgar.mdf):

I have tried to connect with this code:
@{
var base_dados = Database.Open("sisgar");
string query = ("select * from tb_porteiro");
var resultados = base_dados.Query(query);
<table>
@{
foreach ( var linha in resultados )
{
<tr>
<td>@linha.idp </td>
<td>@linha.porteiro </td>
</tr>
}
}
The mdf is in the right folder, the table exists, have the fields

Exists another way to connect to the mdf

Solution
Open Task Manager and look for
sqlsrv.exeprocess and end itDelete all files in the
folder
Then run again.
For more details, you can check the posts below:
1. Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance.
2. Error: Failed to generate a user instance of SQL Server
Workaround
If the solution not works for you. Please try to attach your mdf file to your sql server.