How to switch Entity Framework database connected to

If you have a connection to a database through entity framework and you need to switch it to another database (with the exact same structure) you just need to set the Connection.ConnectionString (as seen below).
I had an application where we created a copy of the Master database when setting up a new client.  So using Entity Framework I switched from the master database to the client (depending on what the admin was doing).

using (MasterEntities aEntities = new MasterEntities())
{//Switch db connected to

 

aEntities.Database.Connection.ConnectionString = aEntities.Database.Connection.ConnectionString.Replace("OldDatabaseName", "NewDatabaseName");

      //Some Query

}

[tweetmeme only_single=”false”]
Advertisements

Access denied error with MVC site

If your new MVC site throws an access denied error you probably don’t have windows authentication setup.

The first thing is checking if you are hosting your site on IIS 7 or better. (Otherwise this section won’t help).
Right click on your project in Visual Studio, click Properties.
Click the web tab.
If you Use IIS Express checked THEN SKIP TO THE BOTTOM OF THIS ARTICLE.
image

Open IIS Manager (Start –> Administrative Tools –> Internet Information Services (IIS) Manager).
Navigate to your website (usually Default Web Site) and click on it.
image

Double click Authentication.
If you don’t see Windows Authentication then it’s not setup.
image

See this post about enabling windows authentication.
(You may need to close IIS Manager and reopen it).

4. In the Actions pane, click Enable to use Windows authentication.
image

5. On the Authentication page, select Anonymous authentication.

6. In the Actions pane, click Disable to disable anonymous authentication.
image

Here is how to enable windows authentication on IIS Express in Visual Studio 2012.
1. Click on your project in the Solution Explorer to select the project.
2. If the Properties pane is not open, open it (F4).
3. In the Properties pane for your project:
a) Set “Anonymous Authentication” to “Disabled”.
image

b) Set “Windows Authentication” to “Enabled”.
image

[tweetmeme only_single=”false”]

How to setup windows authentication on Windows

Here on the steps to enable windows auth on Windows 7:
a) In Control Panel open “Programs and Features”.
image

b) Select “Turn Windows features on or off”.
image
c) Navigate to Internet Information Services > World Wide Web Services > Security
and make sure the Windows authentication node is checked.
image

To enable Windows authentication on Windows Server:
a) In Server Manager, select Web Server (IIS) and click Add Role Services.
b) Navigate to Web Server > Security
and make sure the Windows authentication node is checked.

[tweetmeme only_single=”false”]

Enabling SQL Tracing on a TFS 2010 database

If you’ve ever had trouble setting up TFS 2010 (errors, baby that’s what I’m talking about) you might like to have SQL Tracing on TFS database for more info.  By default you don’t have it (and you need a regedit to get it).
(This also refers to the program from SysInternals that is a free download “ DeBug View”)

How about an example!

Let’s say in the Advanced Configuration Wizard you type in the wrong name of the SQL Server Instance you want to use. The error message you will get is:

clip_image002

But look at some of the trace output:

[6468] [Verbose@18:45:56.241] Checking if database master exists on SQL connection Data Source=HORVICKVM-DEV2;Integrated Security=True
[6468] [Error @18:46:21.381] Microsoft.TeamFoundation.Admin.TfsAdminException: TF255049: A connection cannot be made to the server that is running SQL Server. Verify that you have entered the correct name for the server, including the instance name, that the server you are attempting to connect to is online, and that you have the required permissions to connect. —> Microsoft.TeamFoundation.Framework.Server.DatabaseConnectionException: TF246017: Team Foundation Server could not connect to the database. Verify that the server that is hosting the database is operational, and that network problems are not blocking communication with the server. —> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

What’s so great?

Well first the error message is highlighted red (in DbgView it will be white text on a red background given my example above – but here I made it red text on white to be more readable in the blog post).

Also the message contains the true .NET Exception that occurred and the connection string we used. Those are two really useful pieces of information when you are trying to figure out what is happening. More specifically it even told you how the check was being performed (“Checking if database master exists…”).

Oh No! I have Warnings and Errors!

Well – did anything actually go wrong with your configuration? If not – then it’s probably nothing to worry about. We have some messages that come out as warnings or errors but are in fact benign.

Why Don’t I See Server Messages?

This blog post only explains how to enable tracing for the TFS 2010 admin tools. It does not explain how to enable tracing for the server components, SQL, the job service (though we do trace the job service output when a servicing operation is initiated by an admin tool such as tfsmgmt.exe or tfsconfig.exe). This trace data will provide you zero insight into your server’s realtime activities. Only configuration actions.

Here are the steps you need to follow.

Enabling Tracing

  1. Close the TFS 2010 configuration wizard or admin console (if running).
    1. We only check the trace level when the program starts up. So if it’s already running and you haven’t done this – you need to close it!
    2. Oh – if you are running the TFS 2010 Setup and are at the window where you are asked if you want to launch the configuration tool – you can update the registry key now and not need to close the configuration wizard since the MSI and configuration phases are separate executables).
  2. Enable verbose tracing by doing the following:
    1. Open Regedit (must be an Administrator)
    2. Find the key HKLM\SOFTWARE\Microsoft\TeamFoundationServer\10.0\Admin
    3. Edit the value “TraceLevel” (DWORD) – set to “4” (it is probably “0”)
    4. You can do this from an admin command prompt by running:
      1. reg.exe add HKLM\SOFTWARE\Microsoft\TeamFoundationServer\10.0\Admin /t REG_DWORD /v TraceLevel /d 4 /f
  3. Run DbgView
    1. You can download DbgView from http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
      1. You can also run it right from the website at this link: http://live.sysinternals.com/Dbgview.exe
  4. By default DbgView is ready to go – Under the “Capture” menu make sure that “Capture Win32” (CTRL+W), “Pass-Through” and “Capture Events” (CTRL+E) are enabled. Nothing else is needed.
    1. clip_image002[4]
  5. Start up the TFS Administration Console (or configuration tool, whatever).

When the config tool starts you should see some data streaming in to DbgView – it should look something like this:

[6468] Admin TraceLevel = Verbose

Show Me The Errors!

  1. Bring up the filters screen in DbgView (CTRL+L or clicking on this button: clip_image004).
  2. Leave the Include and Exclude fields alone.
  3. In the Highlight section leave “Filter 1” selected and type “[Error” into the Red text area (please note that I typed an opened brace but not a closing one and also do not type the double quotes). It should look like this:
    1. clip_image006
  4. Now change “Filter 1” to “Filter 3” (Nothing magically about 3. I use 3 because it’s kind of orange – I don’t like the purple of “Filter 2” and I don’t want to get into color changing in this blog post – you want to change colors? Go nuts.).
  5. Add the string “[Warning” – again, no closing brace and don’t use the double quotes.
    1. clip_image008
  6. Now if you happen to have an error it will be much easier to detect.
[tweetmeme only_single=”false”]

Tulsa SQL Server User Group presentation

I presented at the Tulsa SQL Server user group on 4-9-2013.  I spoke about support scripts for the app developers sql server toolbox.
For those that came, thanks for showing up and listening to me.

Attached is my presentation.
SQL Server Support Scripts

You will have to rename it from .pptx to .zip.
Wordpress won’t let up upload a zip file.
I wanted to include the sql scripts with the powerpoint presentation.

If you just want the powerpoint here is the link for that.
SQL Server Support Scripts

I’d love to hear any thoughts or feedback about it.

Where can I find Active Directory Tools

In order to get the option to manage users, computers, etc in AD you need to have the AD tools installed.
This is a 2 step process.

The first step is you need to install the Remote Server Administration Tools for Windows 7 with SP 1.

The second step is to enable the AD tools in control.
From the control panel, click Programs and Features.
Select Turn Windows Features on or off (on the left side)
image

Under Remote Server Administration Tools –> Role Administration Tools, select AD DS and AD LDS Tools.
Select the Active Directory Administrative Center.
Click Ok.
image

 

Now under the Start menu –> Administrative Tools you will find your AD tools
image

Live Mesh sync error

I was using live mesh to sync my favorites, and some folders.  I had to switch live mesh accounts and when I tried to use mesh with my new I got the following error
“This folder can’t be synced. It is already being synced, contains folders that are already being synced, or is in a folder that is already being synced. Please select a different folder or move this folder to another location.”

In order to solve this I deleted everything I could from the directory C:\users\<MYUSERNAME>\AppData\Local\Microsoft\Windows Live Mesh

Then I stopped and restarted live mesh.  Then I had to setup what was synced, but I had no trouble after that.

Command line to lock Windows 7 PC

For security reasons, I would like my PC to lock after I walk away from it.
I have a problem where on of Windows 7 PC’s doesn’t start the screensaver (which I have set to require me to log back in).

Yeah I can hit windows+L to lock my PC, but I forget sometimes (like when I go home for the weekend).

So I setup a schedule task to run the following command:

%windir%\System32\rundll32.exe user32.dll,LockWorkStation

The task runs every day after 5:30 PM, so my PC will always end up locked.

[tweetmeme only_single=”false”]

Check if a SharePoint list exists

SharePoint 2007 doesn’t have any methods to tell you if a list exists.
So you have to try to get an instance of the list and catch the error.

So here the method I use to check if a list exist.
You pass in the SPListCollection and the title of the lists.

//Check if a list already exists
private static bool listExists(SPListCollection collection, string title)
{
try
{
foreach (SPList list in collection)
{
if (list.Title == title)
{
return true;
}
}
return false;
}
catch (Exception)
{

throw;
}
}

Do’s and Don’ts on SharePoint

Once you get into using SharePoint, you get an idea of how big it is.
At that point you start to come across some questions of what’s the best way to do something.

I found a great article of some lessons learned about SharePoint.
http://weblogs.asp.net/bsimser/archive/2011/01/12/do-s-and-don-ts-building-sharepoint-applications.aspx

These aren’t best practices, but more along the lines of tips.

The only tip I might add, is not to think before you create a new site.   Only create a new site if you need all of it.
I have seen many times people create multiple sites, when they only need a couple of lists/libraries.
So ask yourself about if you are really going to need a whole site for what your doing.