Easiest way to debug Seed-method in EF Code-first migrations in Configuration.cs when running Update-Database

Sequence contains more than one element

This post describes the easiest way to debug the issues that may stop your Seed-method in Configuration.cs from going through. The solution here shows you, how you can get a little bit more information out of the process, without attaching the debugger (there’s another blog post for that!)

Description

Entity Framework’s code-first migration’s are a beautiful and easy way of managing database schema changes and populating some preliminary data there. Personally I also sometimes use the method for adding some enrichment to data or or custom property values mapping that would otherwise require an additional/external console program.

Problem: running the Seed-method is by default undebuggable

Okay – so seeding data is cool. That’s fine and dandy, but debugging the issues in the function while running Update-Database is NOT so cool. You only get the stacktrace and exception message – and that’s pretty ugly.

Continue reading

Launching a new debugger instance from code in Visual Studio

Sequence contains more than one element

This post describes a quick solution to launching a new Visual Studio instance for debugging the code. Where I’ve found this exceptionally useful, has been in debugging code-first migration’s (one of the ways for database initialiation in .NET) Seed-method. It is by default undebuggable, as when you are running Update-Database you can’t really use a -debug switch or anything, and there’s really no way to launch the debugger. Hence the best you can do is using -verbose to get more information.

However, if you actually want to see what is happening in the code, here’s the solution.

Continue reading

Using Dispatcher to update values in GUI elements from a background thread

System.InvalidOperationException'

Quick tip:
If you’re developing something like a WPF app and you’ll need to update values on the User Interface based on a long-running operation that runs in a background thread (like depicted here), you’ll probably need to use Dispatcher, or otherwise you’ll run into issues with the GUI elements being owned by another thread, and therefore forbidding access to them. This can result in an error like this:

Luckily, there’s a quick workaround available. Read on!

Cue the Dispatcher

Continue reading

Updating files in an App Part (SharePoint Add-in)

SharePoint_aint_broken

Luckily, SharePoint Add-ins (or App Parts, like they were called earlier) are slowly getting killed and rooted out of all the sites they once were deployed to – and I don’t think anyone’s going to miss them. However, as so often happens with legacy implementations, there will still be thousands of sites, where SharePoint administrators and developers will be responsible for maintaining and developing the solutions further. This will occasionally require updating app parts, which is a process that kind of sucks. Here I’ll try to simplify the process.

Problem(s)

Not all the files in an app part are updated during the deployment and upgrade of the app. This is difficult to debug and leads to new functionality or enhancements not being applied to the target system.

Continue reading

Fixing the error: “Column XX in table dbo.YY is of a type that is invalid for use as a key column in an index.”

entity-framework-logo

While using Entity Framework and code-first migrations, EF creates the indexes for you – but what if you need to create a custom one? Usually, it’s easy – you just add the following annotation to the columns you’ll be using:

(example stripped of extra code and other columns for clarity)

And after adding the migration (Add-Migration…) you get something like this:

But what if, when running Update-Database, you get an error like:

There’s a quick and simple solution.

Continue reading

Fixing “An error occurred while updating the entries” while running code-first migrations in MVC 5 app

Update-Database error

This post describes an issue with EF’s code-first migrations, when mapping between DB’s DateTime and C#’s DateTime kind of fails, and results in Update-Database cmdlet failing.

Symptoms

While running Update-Database in a code-first ASP.NET MVC5 + EF6 -project, you get a following (or similar) error:

Continue reading

Simplest way to create a thread on SharePoint

Stock photo from pixabay.com

This post describes the (probably) easiest and most straightforward way of creating a new Thread in your SharePoint (or any other .NET) server-side / desktop code.

Solution: how to create a Thread

Let’s face it – one should not create new Threads lightly when developing SharePoint solutions, but sometimes it’s difficult to avoid. This one time we were developing a pretty simple functionality, where we needed to create a few fields on a certain title when user activated a feature. However, because of the complexity of the environment, we encountered problems changing the title of the new field. Now, it’s usually pretty simple, but this time some other functionality, most likely developed by someone else, was changing it to the internal field name – which doesn’t look to good.

To be honest, we’re not even sure, why this worked. We suspected it might be because of the current culture of the thread, but changing it didn’t have any effect. Any way, we’re glad this worked, whatever the reason for that is.

Programmatically creating SPFields with readable internal names

SharePoint Field Name Problems

This post is about a small programmatic workaround to creating new SPFields for SPLists in SharePoint with human-readable internal names. This is mainly an usability improvement for your editors, but they’ll probably appreciate it!

Problem: non-readable internal names for SharePoint list fields

When you create a new field on SharePoint, SharePoint accepts the following syntax:

However, this will result in the internal name to be something like “Field_x0020_name_x0020__x002d__x” which is hardly readable, but rather quite horrible to look at, or use anywhere at all.

Luckily, there’s a nice and straightforward way to solve this issue and produce nice and readable internal names for SharePoint list fields!

Solution

If you need to get readable internal names (like when your editors actually use the internal names for something) for your fields, you can use a code similar to this:

Continue reading