I'm trying to use a code first approach to create a brand new database in Entity Framework to test my WPF Prism application. A new database should be created whenever the application is launched, due to this being a new project that will be changing rapidly during development.
I've run into the following exception when trying to create the database:
"Cannot create file 'C:\Users\Edvard\SharedResources.DataAccess.EFDbContext.mdf' because it already exists. Change the file path or the file name, and retry the operation.\r\nCREATE DATABASE failed. Some file names listed could not be created. Check related errors."} System.Exception {System.Data.SqlClient.SqlException}
This is what the datacontext constructor looks like:
public EFDbContext()
{
Database.SetInitializer<EFDbContext>(new CustomInitializer<EFDbContext>());
}
As you can see above, I'm calling a custom initializer so that I can seed the database with information:
public class CustomInitializer<T> : DropCreateDatabaseAlways<EFDbContext>
{
protected override void Seed(EFDbContext context)
{
context.Users.Add(new User
{
SaveableClaims = new List<SaveableClaim>
{
new SaveableClaim { Type = CustomClaimTypes.Username, Value = "Mark" },
new SaveableClaim { Type = CustomClaimTypes.HashedPassword, Value = "MB5PYIsbI2YzCUe34Q5ZU2VferIoI4Ttd+ydolWV0OE=" },
new SaveableClaim { Type = CustomClaimTypes.FirstName, Value = "Mark" },
new SaveableClaim { Type = CustomClaimTypes.LastName, Value = "Johnson" },
new SaveableClaim { Type = CustomClaimTypes.UIAccess, Value = UIAccessTypes.SearchMenuRead }
}
});
context.Users.Add(new User
{
SaveableClaims = new List<SaveableClaim>
{
new SaveableClaim { Type = CustomClaimTypes.Username, Value = "John" },
new SaveableClaim { Type = CustomClaimTypes.HashedPassword, Value = "hMaLizwzOQ5LeOnMuj+C6W75Zl5CXXYbwDSHWW9ZOXc=" },
new SaveableClaim { Type = CustomClaimTypes.FirstName, Value = "John" },
new SaveableClaim { Type = CustomClaimTypes.LastName, Value = "Markson" }
}
});
base.Seed(context);
}
}
Am I doing something wrong the in the above code that might be causing this? To provide some additional detail, I'm using localdb. This is the App.config file:
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
If there's more information I can provide please let me know. Thanks for any help you can provide!