LINQ and Entity Framework Errors

Possibly save 4 hours of your time: When working with LINQ and Entity Framework 6, you may encounter the following errors.

  1. Only parameterless constructors and initializers are supported in LINQ to Entities.
  2. The ‘Distinct’ operation cannot be applied to the collection ResultType of the specified argument. Parameter name: argument
    This happens on Union operation of two entities with different schemas
  3. An error occurred while preparing the command definition. See the inner exception for details.
    This happens on Union operation of Concat operation on two entities with different schemas
  4. System.InvalidOperationException : The specified cast from a materialized ‘System.Int32’ type to the ‘System.String’ type is not valid.
    This happens on Union or Concat operation on two entities with different schemas.

First one is obvious, you must have parameterless constructor when selecting new object transformation.

from i in dbcontext.TableName

select new EntityObjectName() { ID = i.ID }

Second and Third one is similar. When you select new object with properties that are in array form, you may get these errors. One idea would be to simply change this to IEnumerable or IList.

Fourth, this one took me awhile to figure out. It basically happens when the property being transformed came from two different tables and have different data types on the database backend. A simple fix is to check for null and turn into empty string on both the union/concat select. This seems to fix the issue.

from i in dbcontext.TableName

select new EntityObjectName() { Value = i.Value ?? “” }


from j in dbcontext.TableName2

select new EntityObjectName() { Value = j.Value2 ?? “” }

Programming Entity Framework: Code First: Creating and Configuring Data Models from Your Classes


Make methods accessible to unit tests

Possibly save 1 hour of your time: If you would like to create unit tests for your library, you can create the unit tests in a different project to separate the tests and the code.

1. Typically, a good convention is use the same project name and append dot Tests. i.e. MyProjectName.Tests

2. Then you will need to add reference to your project.

3. To access the methods in your project from the tests project, you will need to mark them internal access unless it is already marked as public access.

4. In the AssemblyInfo.cs of your project, you need to add the following the make your project visible to your tests project.

   [assembly: InternalsVisibleTo(“YourProjectName.Tests”)]

There are other methods of testing your protected or private methods, but I find this solution the easiest to configure and maintain.

Publish a Nuget package to your own server

Possibly save 1 hour of your time:

1. Download Nuget and set environment variables path where the nuget.exe is found

2. Open command prompt and cd to the .csproj folder.

Run the following command to create a nuget specification template.

nuget spec

3. Open command prompt. Then change directory to go to the folder where the YourProjectName.csproj is. Then enter the following to package your csproj.

    nuget pack YourProjectName.csproj

Next, you will push this to your server location

    nuget push YourProjectName. -Source \\serverLocation

The version number is based on your assembly info file.

Resource Link:



Execute PowerShell Script

Possibly save 1 hour of your time:

  1. Use ./ (dot and slash and PowerShellScriptName) to run the command in the current path folder. You don’t need to enter .ps1

Often times you will type cd to change directory to the folder where the PowerShell script exists. Then you type the script name and it gives you error that the command is not recognized.

PS C:\Scripts> HelloWorld

PS C:\Scripts> ./HelloWorld