The Entity Framework provider … could not be loaded

When you create a new Microsoft Visual Studio 2013 project that uses the Entity Framework you may get the following error message:
The Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config file for the ADO.NET provider with invariant name ‘System.Data.SqlClient’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

  • Add the EntityFramework NuGet package to the Data Access project that accesses the database and the Web Application or Web Service project that calls the project
    • Right click the project in Solution Explorer
    • Select Manage NuGet Packages
    • On the left side select Online
    • On the right side enter EntityFramework into the search field
    • Click Install next to the EntityFramework package
  • Make sure your app config file contains the following lines:

 

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="v11.0" />
    </parameters>
  </defaultConnectionFactory>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
  • Your project should contain a reference to EntityFramework.SqlServer.dll
  • Add the following line to the constructor of the class that accesses the database
var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
  • Add the System.Data.Entity reference to the project accessing the database

How to migrate the Visual Studio 2013 user database to SQL Server 2008

When you create a new web application in Microsoft Visual Studio 2013 the user database it creates is version 706. SQL Server 2008 can only open databases up to version 663 or earlier. To migrate the new Visual Studio 2013 databases to SQL Server 2008 script the database tables and create them again in the SQL Server 2008 Management Studio.

  • Start Visual Studio 2013
  • Create a new web application with user authentication
  • On the left side select the Server Explorer
  • Click the arrow next to the Data Connections
  • Click the arrow next to DefaultConnection
  • Click the arrow next to Tables
  • Right click each table and select Open Table Definition

visual studio script database table

At the bottom of the screen the table definition script appears

visual studio script database table result

You have to create the tables in the appropriate order, to satisfy the foreign key references. The script below creates the tables in the right order:

CREATE DATABASE your_database
GO
USE your_database
GO

--  ---------------------------------------------------------------------

CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId]    NVARCHAR (150)  NOT NULL,
    [ContextKey]     NVARCHAR (300)  NOT NULL,
    [Model]          VARBINARY (MAX) NOT NULL,
    [ProductVersion] NVARCHAR (32)   NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY CLUSTERED ([MigrationId] ASC, [ContextKey] ASC)
);
GO            

--  ---------------------------------------------------------------------

CREATE TABLE [dbo].[AspNetRoles] (
    [Id]   NVARCHAR (128) NOT NULL,
    [Name] NVARCHAR (MAX) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetRoles] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO

--  ---------------------------------------------------------------------

CREATE TABLE [dbo].[AspNetUsers] (
    [Id]            NVARCHAR (128) NOT NULL,
    [UserName]      NVARCHAR (MAX) NULL,
    [PasswordHash]  NVARCHAR (MAX) NULL,
    [SecurityStamp] NVARCHAR (MAX) NULL,
    [Discriminator] NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO

--  ---------------------------------------------------------------------

CREATE TABLE [dbo].[AspNetUserClaims] (
    [Id]         INT            IDENTITY (1, 1) NOT NULL,
    [ClaimType]  NVARCHAR (MAX) NULL,
    [ClaimValue] NVARCHAR (MAX) NULL,
    [User_Id]    NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUserClaims] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_User_Id] FOREIGN KEY ([User_Id]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO

--  ---------------------------------------------------------------------

CREATE TABLE [dbo].[AspNetUserLogins] (
    [UserId]        NVARCHAR (128) NOT NULL,
    [LoginProvider] NVARCHAR (128) NOT NULL,
    [ProviderKey]   NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUserLogins] PRIMARY KEY CLUSTERED ([UserId] ASC, [LoginProvider] ASC, [ProviderKey] ASC),
    CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
    ON [dbo].[AspNetUserLogins]([UserId] ASC);
GO

--  ---------------------------------------------------------------------

CREATE TABLE [dbo].[AspNetUserRoles] (
    [UserId] NVARCHAR (128) NOT NULL,
    [RoleId] NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUserRoles] PRIMARY KEY CLUSTERED ([UserId] ASC, [RoleId] ASC),
    CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[AspNetRoles] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_RoleId]
    ON [dbo].[AspNetUserRoles]([RoleId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
    ON [dbo].[AspNetUserRoles]([UserId] ASC);
GO

Execute the script in the SQL Server Management Studio

Warning: Attempt to present … on … while a presentation is in progress!

There is a warning message in Xcode, the Apple iOS development environment that can have many different causes.

Warning: Attempt to present … on … while a presentation is in progress!

In my case Xcode displayed the warning because a button called the same view twice.

xcode multiple calls to viewcontroller

When I deleted the action associated with the Touch Up Inside event the warning disappeared.

Xcode app development troubleshooting

This post is a collection of common error messages you may receive during application development in Xcode.

Error message

Receiver ‘NSManagedObjectContext’ for class message is a forward declaration

Solution

Add

#import <CoreData/CoreData.h>

to the … – Prefix.pch Prefix header file in the Supporting Files folder


 

QWERTY Hungarian keyboard on an English Macintosh keyboard

The English characters on the English and Hungarian keyboards are almost at the same place. The exceptions are the Z and Y keys, those are swapped.

When you activate the Hungarian keyboard on a Mac with an English keyboard, the Z key will display Y and the Y key will display Z on the screen. That is the German (and Hungarian) QWERTZ keyboard layout. On an English keyboard, it is much easier to use the QWERTY layout for the Hungarian language too.

On macOS Sierra (10.12.16)

Sierra already contains the Hungarian QWERTY keyboard

To select the QWERTY Hungarian layout

  1. On the settings page select the Keyboard
  2. On the Input Sources tab click the + sign
  3. Select the Hungarian – QWERTY layout and click the Add button

For earlier Mac OS versions

In the past, the following page contained a Hungarian layout for English keyboards at http://blog.felho.hu/hungarian-unicode-qwerty-keyboard-layout.html by “Felho”

The page is not available anymore, but I have found the keyboard layout files in the Wayback Machine internet archive at https://web.archive.org/web/20120315000000*/http://blog.felho.hu/wp-content/felhokeylayout.zip

I have downloaded the ZIP file from the archive and you can download it below in step 1.

Get a QWERTY Hungarian layout

To set up the QWERTY Hungarian keyboard layout

  1. To download the keyboard file ZIP archive click this link: felhokeylayout.zip
  2. Click the Downloads shortcut on your desktop
    open downloads folder
  3. Click the downloaded layout file to open it
    hungarian qwerty keyboard open downloaded file
  4. Select the two files
    hungarian qwerty keyboard files
  5. Copy the two files into the Macintosh HardDrive -> Keyboard Layouts folder
    keyboard layouts folder
  6. Log out of the computer
  7. When you log in again your Mac will find the new keyboard layout
  8. In the Personal section of the System Preferences select Language and Text
    language and text
  9. On the Input Sources tab select the Hungarian PRO FB keyboard
    hungarian qwerty keyboard selection

On the top of the screen between the battery status and the time you will see the name of the keyboard layout you currently use.

keyboard selection

Click the icon to select the other layout.

If you want to create your own special keyboard layout

If you want to create your own keyboard layout, there is a small application that can create a customized keyboard layout: Ukelele.

You can download it from their website at http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=ukelele.

 

Save an image in the Xcode iPhone simulator

When you are testing an app in the Xcode iOS Simulator sometimes you need to select an image in the photo library. To add images to Photos in the Xcode iOS Simulator

  • Run you app in the iOS iPhone Simulator
  • In the Hardware menu select Home to hide your app
  • Open the Safari browser in the iOS Simulator
  • Navigate to a page with images
  • Press and hold the mouse on the image for a few seconds
  • A pop-up window appears
    ios simulator save image
  • Select Save Image to save the picture in the photo library of the Xcode iOS Simulator

How to lock your Macintosh and Windows computer

Always lock your computer to protect your secrets in the office while you are getting a cup of water from the kitchen.

To lock a Macintosh computer

  • Press the Control – Command – Q keys, or
  • In the upper left corner click the Apple logo and select Lock Screen

To lock a Windows computer

In Windows, when you press the Windows-L key combination the login window appears

Microsoft Azure – Storage – Part 5 – Delete blobs with a .NET application

In the prior article of the Microsoft Azure storage series we have downloaded a file (blob) from the container. In this part we will add a method to delete a blob in the Azure storage container.

Delete a blob  in the storage container

Add the DeleteBlob method to the Azure_Helper

  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add the following method to the class
/// <summary>
/// Deletes a blob in the Azure storage
/// </summary>
/// <param name="sContainerPath"></param>
/// <param name="sBlobName"></param>
public void DeleteBlob(string sContainerPath, string sBlobName) {

    // Create the blob client.
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    // Retrieve reference to a blob named "myblob.txt".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(sBlobName);

    // Delete the blob.
    blockBlob.Delete();
}

Call the Azure_Helper from the Console Application

  • Open the Program.cs file in the Azure_ConsoleApplication
  • Add the following to the end of the Main method
// Delete the blob from the Azure storage
azureHelper.DeleteBlob(sContainerPath, sBlobName);

When you are testing the application the first time you can place a break point to the line that calls the DeleteBlob method and use the Azure manager  at https://manage.windowsazure.com  to follow the creation and deletion of the blob in the Azure storage container.

Microsoft Azure – Storage – Part 4 – Download files with a .NET application

In the third part of the Microsoft Azure Storage series we have extended our .NET application to list the contents of a container.  Today we will add a method to our application to download files from the storage container.

Download blobs from the Microsoft Azure storage

Create the DownloadFile method in the Azure Helper

  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add a new method to the class
/// <summary>
/// Downloads a blob from the storage container and saves it in the file system
/// </summary>
/// <param name="sContainerPath"></param>
/// <param name="sBlobName"></param>
/// <param name="sTargetFilePath"></param>
public void DownloadFile(string sContainerPath, string sBlobName, string sTargetFilePath) {

    // Create the blob client.
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    // Retrieve reference to a blob
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(sBlobName);

    // Save blob contents to a file.
    using (var fileStream = System.IO.File.OpenWrite(sTargetFilePath)) {
        blockBlob.DownloadToStream(fileStream);
    }

}

This method will download the file from the Microsoft Azure storage and save it on a disk.

Add code to the console application to call the DownloadFile method

  • Open the Program.cs file in the Azure_ConsoleApplication
  • Add the following lines to the end of the Main method
string sTargetFilePath = @"D:\MediaFiles\Audio\DownloadedMusic.wav";

// Download the file from the Azure storage and save it
azureHelper.DownloadFile(sContainerPath, sBlobName, sTargetFilePath);

Test your application

Run your application. A new file should appear in the specified target folder.

In the next article of the Azure storage series we will learn how to delete a blob from the storage container.

Microsoft Azure – Storage – Part 3 – List the contents of the container with a .NET application

In the second part of the Azure Storage series we have developed a .Net application that can create a Microsoft Azure storage  container and upload files into it. Today we will extend the functionality of our application to list the files in the container.

List the files in the container

First let’s create a class that represents the Azure Blob. This way we can isolate our console application from the Azure Storage system, so in the future it can work with any cloud storage systems.

Add a file to the Azure_Helper project to store enumerations

  • Right click the Azure_Helper project and select Add -> Class
  • Name the class Enums
  • Add an enumeration outside of the class definition. The file should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nsAzure_Helper {

    public class Enums {
    }

    public enum ItemType {
        undefined
        , BlobDirectory
        , BlockBlob
        , PageBlob

    }
}

Add a class to the Azure_Helper to represent a file or directory in the cloud

  • Right click the Azure_Helper project and select Add -> Class
  • Name the class CloudItem
  • To make the class public enter the word public in front of the word class
public class CloudItem
  • Add public properties to the class. The class definition should look like this:
public class CloudItem {

    public long Length { get; set; }
    public Uri Uri { get; set; }
    public ItemType Itemtype { get; set; }

}
  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add a new method to list the content of the container
/// <summary>
/// Returns the list of cloud items in the container
/// </summary>
/// <param name="sContainerPath"></param>
/// <returns></returns>
public List<CloudItem> GetContainerItems(string sContainerPath) {

    List<CloudItem> itemList = null;

    // Create the blob client. 
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    if (null == container) {
        // No container found
        return itemList;
    }

    // Create an instance of the item list
    itemList = new List<CloudItem>();

    // Loop over items within the container and output the length and URI.
    foreach (IListBlobItem item in container.ListBlobs(null, false)) {

        // Create a new instance of the Cloud Item
        CloudItem cloudItem = new CloudItem();

        if (item.GetType() == typeof(CloudBlockBlob)) {
            // This is a Block Blob

            CloudBlockBlob blob = (CloudBlockBlob)item;

            cloudItem.Itemtype = ItemType.BlockBlob;
            cloudItem.Length = blob.Properties.Length;
            cloudItem.Uri = blob.Uri;

        } else if (item.GetType() == typeof(CloudPageBlob)) {
            // This is a Page Blob

            CloudPageBlob pageBlob = (CloudPageBlob)item;

            cloudItem.Itemtype = ItemType.PageBlob;
            cloudItem.Length = pageBlob.Properties.Length;
            cloudItem.Uri = pageBlob.Uri;

        } else if (item.GetType() == typeof(CloudBlobDirectory)) {
            // This is a Directory

            CloudBlobDirectory directory = (CloudBlobDirectory)item;

            cloudItem.Itemtype = ItemType.BlobDirectory;
            cloudItem.Uri = directory.Uri;

        }

        // Add the item to the list
        itemList.Add(cloudItem);

    }

    return itemList;

}

The method above returns a list of Cloud Items.

Let’s call the method from our Console Application

  • Open the Program.cs file in the Azure_ConsoleApplication project
  • Add the following code to the existing lines
// Get the list of items in the Container
List<CloudItem> itemList = azureHelper.GetContainerItems(sContainerPath);
if (null != itemList){

    foreach (CloudItem item in itemList){

        if (ItemType.BlockBlob == item.Itemtype) {

            Console.WriteLine("Block blob of length {0}: {1}", item.Length, item.Uri);

        } else if (ItemType.PageBlob == item.Itemtype) {

            Console.WriteLine("Page blob of length {0}: {1}", item.Length, item.Uri);

        } else if (ItemType.BlobDirectory == item.Itemtype) {

            Console.WriteLine("Directory: {0}", item.Uri);
        }

    }
}

Console.WriteLine("Press a key to exit");
Console.Read();

The Program.cs file should look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using nsAzure_Helper;

namespace Azure_ConsoleApplication {
	class Program {
		static void Main(string[] args) {

			// Create an instance of the Azure Helper
			Azure_Helper azureHelper = new Azure_Helper();

			string sContainerPath = "test1";
			bool bMakeContainerPublic = true;
			string sBlobName = "blob1";
			string sSourceFilePath =  @"D:\MediaFiles\Audio\Music.wav";

			// Create a public container
			azureHelper.CreateContainer(sContainerPath, bMakeContainerPublic);

			// Upload an audio file
			azureHelper.UploadFile(sContainerPath, sBlobName, sSourceFilePath);

			// Get the list of items in the Container
			List itemList = azureHelper.GetContainerItems(sContainerPath);
			if (null != itemList){

				foreach (CloudItem item in itemList){

					if (ItemType.BlockBlob == item.Itemtype) {

						Console.WriteLine("Block blob of length {0}: {1}", item.Length, item.Uri);

					} else if (ItemType.PageBlob == item.Itemtype) {

						Console.WriteLine("Page blob of length {0}: {1}", item.Length, item.Uri);

					} else if (ItemType.BlobDirectory == item.Itemtype) {

						Console.WriteLine("Directory: {0}", item.Uri);
					}

				}
			}

			Console.WriteLine("Press a key to exit");
			Console.Read();

		}

	}
}

In the next article we will learn how to download files from the Microsoft Azure storage.