1

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!

7
  • Are you using a connection string in the app.config? Commented Aug 2, 2015 at 20:57
  • I'm not @dustmouse, just indicating the connection factory. Commented Aug 2, 2015 at 21:24
  • 1
    Does that file exist, and if so, if you blow it away, do you still get the error? Commented Aug 2, 2015 at 21:25
  • 1
    @dustmouse, you were spot on! Found the files and deleted both, which fixed the problem. Feel free to add an answer for me to mark as correct :) Commented Aug 2, 2015 at 23:12
  • Does it generate the same error on subsequent runs? Commented Aug 2, 2015 at 23:12

1 Answer 1

2

Find the file located at:

'C:\Users\Edvard\SharedResources.DataAccess.EFDbContext.mdf'

And delete it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.