Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

c# - Entity Framework 4.3 doesn't create database

I created new project and added the newest entity framework to it (version 4.3). I created classes and the context as in previous EF versions. However, during the very first run when the database should be created (in my case it is SQL Server 2005), I'm receiving the following error:

An error occurred while executing the command definition. See the inner exception for details.

With the following inner exception:

Invalid object name 'dbo.__MigrationHistory'.

As I understand, this table is for migrations, but this table does not exist if there is no database. Am I doing something wrong?

More info: For testing purposes I created only one class:

public class Test
{ 
  [Key]
  public int TestId { get; set;}

  public string Name {get; set;}
}

public class Context : DbContext
{
   public Context() : base("MyConnection")
   {
   }

   public DbSet<Test> Tests { get; set;}
}

UPDATE 1

After some tests I realized that application is throwing unhandled exception from visual studio and break in visual studio. The exception was System.Data.EntityCommandExecutionException. Once I ignored that expection and didn't stop code execution, database was created.

UPDATE 2

After another few hours working with database I found out that playing with Enable-Migrations option and Update-Database from console also is solving that issue. It is creating database before application start and don't break in Visual Studio.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Could you try removing your constructor to make EF use it's default connection string.

public Context() : base("MyConnection")
{
}

Failing that, could you try updating your database from the Package Manager Console to see if you get any further information.

Update-Database -Verbose

Possibly unrelated in your case, but I get the same error when using MvcMiniProfiler 1.9. If you are using it too, make sure EF profiling is turned off by commenting out the line:

//MiniProfilerEF.Initialize();

Within the MiniProfiler App_Start.


For others experiencing a similar issue, I have found that reenabling migrations from the Package Manager Console can help in certain cases. Make sure you have a copy of your Migration configuration before doing this.

Enable-Migrations -Force

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...