Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when specifying AttachDbFileName in connection string #2810

Closed
XiaoweiJia opened this issue Aug 10, 2015 · 16 comments
Closed

Error when specifying AttachDbFileName in connection string #2810

XiaoweiJia opened this issue Aug 10, 2015 · 16 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug
Milestone

Comments

@XiaoweiJia
Copy link

XiaoweiJia commented Aug 10, 2015

Edit: look at #2810 (comment) for details on the bug.

When specifying AttachDbFileName in connection string, the following error appear: Unable to Attach database file as database xxxxxxx.
Please advise.

Thanks

@natemcmaster
Copy link
Contributor

Please provide more details about your issue.

@XiaoweiJia
Copy link
Author

I am trying to use migration commands to generate database: "dnx . ef migration apply". I get an error saying the database file cannot be attached as database xxxx but if I remove the "AttachDbFileName" section, it works fine and the database file is created in default location.
Please see my code in ConfigureServices.

public void ConfigureServices(IServiceCollection services)
        {
            var connection = @"Data Source=(LocalDB)\mssqllocaldb;Integrated Security=True;Initial Catalog=SampleDb;AttachDbFileName=D:\SampleDb.mdf";
            services.AddEntityFramework().AddSqlServer().AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connection));
        }

@natemcmaster
Copy link
Contributor

Thanks for updating. This is not a bug in EF 7. We pass-through the connection string to SqlClient. There may be a problem with your localdb setup.

Feel free to re-open if you identify a problem with how EF 7 handles your connection string.

@bricelam
Copy link
Contributor

Cross-reference: Cannot attach database file when using Entity Framework 7 Migration commands (on StackOverflow)

@axelheer
Copy link
Contributor

axelheer commented Aug 30, 2016

I still think this is a bug, or a missing feature compared to EF6. It's quite annoying anyway.

  • EnsureDeleted changes the database name to master but keeps any AttachDbFileName value, which leads to an error since we cannot attach the master database to another file.
  • EnsureCreated opens a connection using the provided AttachDbFileName value, which leads to an error since the file of the database we want to create does not yet exist.

EF6 seems to have some logic to handle this stuff, see SqlProviderServices.DbCreateDatabase. And working with Local DB requires using AttachDbFileName if we don't want to bloat our user profile (as far as I know). Maybe updating SqlServerConnection.CreateMasterConnection to remove AttachDbFileName and SqlServerDatabaseCreator.IsDoesNotExist to accept the other error would be the first step to resolve this?

I posted a hacky workaround for this at the stackoverflow question linked by @bricelam...

@bricelam
Copy link
Contributor

bricelam commented Aug 30, 2016

@axelheer That sounds like a bug. Could you submit a new issue?

@divega
Copy link
Contributor

divega commented Aug 30, 2016

It seems the original report might have just missed a few details, but it could have been the same issue that @axelheer is describing from the beginning. I will just reactivate for now.

@bricelam
Copy link
Contributor

bricelam commented Aug 30, 2016

Beside not stripping AttachDBFileName, we also don't support creating new attached databases. We'll need to produce DDL like the following.

CREATE DATABASE [MyDatabase]
ON (NAME = 'MyDatabase.mdf', FILENAME = 'C:\Path\To\MyDatabase.mdf')
LOG ON (NAME = 'MyDatabase_log.ldf', FILENAME = 'C:\Path\To\MyDatabase_log.ldf');

This also means we'll need to manually resolve the |DataDirectory| substitution string if present in the connection. 😱

@axelheer
Copy link
Contributor

Yeah, makes sense. Thus, we need already three things:

I've already looked into your tests, but I've no idea how to write some kind of integration test to play through this whole scenario.

@bricelam
Copy link
Contributor

FYI, all the logic for resolving paths was in SqlProviderServices.GetOrGenerateDatabaseNameAndGetFileNames for EF6.

@axelheer
Copy link
Contributor

I may be able to solve that. Should I dive into it and prepare a PR?

@bricelam
Copy link
Contributor

That would be awesome if you're willing!

@bricelam bricelam added the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label Aug 31, 2016
@axelheer
Copy link
Contributor

Okay, I'll give it a try then.

@divega divega added this to the 1.1.0 milestone Aug 31, 2016
@divega
Copy link
Contributor

divega commented Aug 31, 2016

Triage: if we don't take a PR (cc @axelheer) to have proper support in 1.1, the scope for the EF team would be to try to throw a better error.

@bricelam bricelam assigned bricelam and unassigned ajcvickers Sep 7, 2016
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 15, 2016
@efidiles
Copy link

efidiles commented Jan 4, 2018

Hey guys,

We just ran into this ourselves; looks like relative paths aren't supported with AttachDbFileName.

System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file '.\OurDbName.mdf' as database 'OurDbName'.

Just wanted to express that here for other people who might be bumping into this. The "fix" is use absolute paths.

@axelheer
Copy link
Contributor

axelheer commented Jan 4, 2018

@efidiles You can use the mighty |DataDirectory| placeholder too (see here).

@bricelam bricelam added the good first issue This issue should be relatively straightforward to fix. label May 31, 2019
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug
Projects
None yet
Development

No branches or pull requests

7 participants