Category Archives: Errors

Entity Framework – The type was not mapped issue solution


While working on the Entity Framework, you may face an issue with the exception saying:

An unhandled exception of type ‘System.InvalidOperationException’ occurred in EntityFramework.dll

The type ‘xxx’ was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

Here is the console based code which can reproduce this exception:


class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.WriteLine("Please enter blog name:");
string blogName = Console.ReadLine();

Blog blog = new Blog();
blog.Name = blogName;
blog.DateCreated = DateTime.Now;
db.Blogs.Add(blog);
db.SaveChanges();

// List all available blogs in the DB
Console.WriteLine("All blogs in the DB:");
foreach (var item in db.Blogs.ToList())
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
}
public class Blog
{
[Key]
public int IDBlog { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public virtual List<Post> Posts { get; set; }
}

public class Post
{
[Key]
public int IDPost { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int IDBlog { get; set; }
public virtual Blog Blog { get; set; }
}

public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
}

Solution:

The issue is POCO classes and DbContext was nested within POCO class. Entity framework does not like nested classes.

Please note: Nested class means, a class defined within another class.

Error free code:


class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.WriteLine("Please enter blog name:");
string blogName = Console.ReadLine();

Blog blog = new Blog();
blog.Name = blogName;
blog.DateCreated = DateTime.Now;
db.Blogs.Add(blog);
db.SaveChanges();

// List all available blogs in the DB
Console.WriteLine("All blogs in the DB:");
foreach (var item in db.Blogs.ToList())
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
}
}

public class Blog
{
[Key]
public int IDBlog { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
public virtual List<Post> Posts { get; set; }
}

public class Post
{
[Key]
public int IDPost { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int IDBlog { get; set; }
public virtual Blog Blog { get; set; }
}

public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}

Hope this helps.

Error: Unable to Open Database Project in Visual Studio 2012


If you upgrade SQL Server 2012 by installing Service pack 1 (SP1), you may encounter following popup when Visual studio solution with “SQL Server Database Projects” is opened:

Unable to Open Database Project in Visual Studio 2012:

This version of SQL Server Data Tools is not compatible with the database run time components installed on this computer.

To fix this problem you need to download and install SQL Server Data Tools update

Installation:

1. Once downloaded, run the file “SSDTSetup.exe”, you will see following screen.

2. Read and agree the terms and conditions and click on Install button.

3. Installation will start. It will download some files and install them.

4. It will take some time for downloading required files depends upon your internet connection.

5. Once it finishes, you need to reboot your system.

6. Click “Restart” to reboot your system.

7. Once restarted, open the project. You will see that “SQL Server Database Projects” is loaded. If not, migrate the database projects, it will solve your problem.

Server Busy Error on Microsoft Windows


Most of the time, when you boot windows, you may find this error with a title as “Server Busy” which says:

“This action cannot be completed because the other program is busy. Choose ‘Switch To’ to activate the busy program and correct the problem.”

Most of the time, when you click “Switch to” button, the start menu comes up. When you click “Retry” button, the box comes back up, and when you click “Cancel” button it goes away. (sometimes you will find that “Cancel” button is disabled.) It also goes away after a few minutes on it’s own, if you do not click anything.

Possibilities:

You have an installed program which configured to access the internet and perform an operation, such as check for updates.

The ‘server busy’ message is appearing because the program is trying to access the Internet before the connection is completely ready.

Suspected Programs would be an antivirus programiTunes, Java Updater, Samsung kies or any other program that has an update options.

If you find one that meets this description, configure the program to update sometime other than when the computer first starts.

Solutions:

1. In Task Manager, select the ‘Server Busy’ program, right-click and select ‘Go to Process’. This should tell you what program is causing the error. Either uninstall the program or configure it to update sometime other than when computer boots.

2. Run ‘msconfig‘ command and un-check any unnecessary programs, but don’t remove something unless you know what it is. A quick Google search can also help you, if you are not sure about any oddly named processes.

3. Create a new user account on Windows with a fresh user account. The process that is starting will not run anymore. Simply migrate over any documents/pictures/music etc to the new account.

4. If the suspected program (particularly ones that have been recently installed or undergone a significant update.) is difficult to uninstall, then try “System Restore” point to backtrack the recent changes.

5. Check “Event Viewer” entries to find the faulty module that could be causing this issue.

Hope this helps.

Visual Studio 2012 Issue/Problem Fix for Error “No exports were found that match the constraint”


Recently after updating my windows 7, I found a issue in my Visual studio 2012. I could open the solution, but I could not open any project files and it was giving me error as:

“No exports were found that match the constraint:
ContractName    Microsoft.VisualStudio.Utilities.IContentTypeRegistryService
RequiredTypeIdentity    Microsoft.VisualStudio.Utilities.IContentTypeRegistryService”

Visual Studio 2012 was not allowing me to create a new project, even I could not open the solution properly (some projects were not loaded).

A flaw in Visual studio may cause it to become unstable, when the .NET 4.5 framework is updated via windows updates.

After looking into couple of forums, I found the workaround to fix the issue.

After installing the Microsoft update, the issue is resolved. (i.e. Update for Microsoft Visual Studio 2012 – KB2781514 )

You can download this update here.

Before installing, just close the Visual studio 2012, install the update and open the Visual studio to see that the issue is resolved.

Hope this will help.