Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Closed Issue: EF6 throws an exception [Invalid column name 'ContextKey'.\r\nInvalid column name 'ContextKey'.] when using DBs that were created with EF5 [594]

$
0
0
Repro steps:

- Create a console application in Visual Studio 2012
- Install official EF5 nuget package to the project
- Copy the code below, and execute
- Verify that a DB with the name "MyTestDB" is created in local SQLExpress
- Install a recent EF6 nuget package (this should remove EF5 package automatically)
- Rerun the code
- Observe the exception on the first SimpleWrite call
- Hit F5 so that the program continues. Hit any key to make another SimpleWrite call
- Observe no exception is received on the second call
- Observe that whenever the application starts fresh, this exception is always hit on the first call

Repro code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace ConfigTest
{
public class MyEntity
{
public int Id { get; set; }
public string Name { get; set; }

public static MyEntity CreateEntity()
{
return new MyEntity { Name = "Test - " + DateTime.Now };
}
}

public class MyContext : DbContext
{
public MyContext(string cs) : base(cs) { }
public DbSet<MyEntity> TestEntities { get; set; }
}

public static class MyTest
{
public static string SimpleWrite()
{
var entity = MyEntity.CreateEntity();

using (var db = new MyContext("MyTestDB"))
{
db.TestEntities.Add(entity);
db.SaveChanges();
}

return "Saved entity " + entity.Name;
}

}

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test #1: " + MyTest.SimpleWrite());
Console.WriteLine("Hit any key."); Console.ReadKey();
Console.WriteLine("Test #2: " + MyTest.SimpleWrite());
Console.WriteLine("Hit any key."); Console.ReadKey();
}
}
}

Stack trace:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)

Comments: This is confirmed as a first chance exception that only manifests when using a debug build of entityFramework in an application and debugging. This does not manifest in the release build from the Nightly.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>