Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework code first update-database fails on CREATE DATABASE

This post has been noted

So has this one

On my dev machine I am trying to recreate my database using update-database from package manager console. I believe I have followed the instructions in the posts cited above.

I get this error message:

A file activation error occurred. The physical file name '\WRDatabase.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

The command I am running is:

update-database -ConfigurationTypeName WRConfiguration  -ConnectionStringName localWR -Verbose

Note in the command above I am passing a connection string name and it appears the connection string is being used because the name of the .mdf file appears in the error message.

Interestingly, I am able to run my code and my database is created but it is created with the wrong name (it is named "VIN.DataModel.WRContext" which is the full namespace and class name of the DbContext). My guess is that when my code runs EF cant find the connection string for creating the database. This is intentional as I will map this context to a database that runs on a server and also to a copy of the db that runs on the local machine (i.e. I will have two connection strings for the same DbContext).

Here is app config:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
</defaultConnectionFactory>
</entityFramework>
<connectionStrings>
     <add name="localWR" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog =WRDatabase;AttachDBFilename=|DataDirectory|\WRDatabase.mdf; Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>

AutomaticMigrationsEnabled is set to false in the constructor of the Configuration class.

Here is the context class:

public class WRContext : DbContext
{
    static WRContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<WRContext, VIN.DataModel.WRMigrations.WRConfiguration>());
    }

    public WRContext()
    {

    }

    public WRContext(string connectionString)
        : base(connectionString)
    { 

    }


    public DbSet<Emp> Emps { get; set; }
    public DbSet<Inv> Invs { get; set; }
    public DbSet<WRConfig> WRConfigs { get; set; }
}  
like image 601
Sam Avatar asked Sep 18 '13 21:09

Sam


People also ask

How do you update a table in Entity Framework first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish. After the update process is finished, the database diagram includes the new MiddleName property.


2 Answers

I found the answer here:

http://kazimnami.azurewebsites.net/techblog/2012/11/24/error-a-file-activation-error-occurred-create-database-failed/

Thank you kazim.

The exception is thrown when the "|DataDirectory|" property in the connection string is not evaluated. To solve the issue, add following line to your code:

AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
like image 60
Sam Avatar answered Oct 22 '22 11:10

Sam


Actually you need to put the line given by OP's answer in the extended xxContext: dbContext class.

public class WRContext : DbContext
{
    public WRContext()
        : base("localWR")
    {
        AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Directory.GetCurrentDirectory());
    }
    public DbSet<Emp> Emps { get; set; }
    //....
}
like image 36
Dush Avatar answered Oct 22 '22 11:10

Dush