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

SQL Server: Error better when no database name #7816

Closed
cstrachan88 opened this issue Mar 9, 2017 · 6 comments
Closed

SQL Server: Error better when no database name #7816

cstrachan88 opened this issue Mar 9, 2017 · 6 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

@cstrachan88
Copy link

I followed along with the VS Launch live stream and tested a .NETCore console app with EFCore, but I am running into an error while deleting the database. Exception below:

System.ArgumentException occurred
  HResult=0x80070057
  Message=The string argument 'identifier' cannot be empty.
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerSqlGenerationHelper.DelimitIdentifier(String identifier)
   at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(SqlServerDropDatabaseOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator.Delete()
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureDeleted()

Steps to reproduce

So I tried it with my own code first, and then copied and pasted the code from this repo in order to factor out anything I was doing wrong (code is from the live stream demo).

I am using a local database file created by going to Server Explorer, right-clicking on Data Connections and hitting, "Add Connection..." I selected the Microsoft SQL Server Database File then chose a file location with everything else in the default configuration.

The connection string I'm using in the code below came directly from the connection string property when I highlighted this created database file.

The only other steps taken were to run an Add-Migration and an Update-Database commands in the Package Manager Console.

using Microsoft.EntityFrameworkCore;
using System;

namespace EFCore_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var db = new BloggingContext();
            db.Database.EnsureDeleted();
            db.Database.Migrate();
        }
    }

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""...\NewDB.mdf"";Integrated Security=True;Connect Timeout=30"; 
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(connectionString);
            }
        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        public int Rating { get; set; }
        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}

Further technical details

  • Package References:
    • Microsoft.EntityFrameworkCore.SqlServer v1.1.1
    • Microsoft.EntityFrameworkCore.Tools v1.1.0
  • Operating system: Windows 10 Pro
  • IDE: Visual Studio 2017
@ajcvickers
Copy link
Contributor

@cstrachan88 EF Core currently doesn't support connection strings that do not specify a database name (initial catalog). If you add one to your connection string, then you should no longer get this exception.

That being said, we should provide a better message when this fails. Assigning to @bricelam for that.

@ajcvickers ajcvickers added this to the 2.0.0 milestone Mar 10, 2017
@ajcvickers ajcvickers changed the title Error: DbContext.Database.EnsureDeleted - Identifier cannot be empty - EFCore 1.1.1 Generate a more useful error when connection string does not contain a database name. Mar 10, 2017
@cstrachan88
Copy link
Author

That makes sense. Thanks. I was able to get it working by adding a database name to the attached database.

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@bricelam bricelam changed the title Generate a more useful error when connection string does not contain a database name. SQL Server: Error better when no database name Jun 2, 2017
@bricelam bricelam added the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label Jun 30, 2017
@bricelam
Copy link
Contributor

bricelam commented Jun 30, 2017

cc @axelheer (in case he's interested in submitting a PR for this. He's worked in this area before.)

@axelheer
Copy link
Contributor

@bricelam thanks for mentioning me! But I'm quite busy at the moment. :(

@bricelam
Copy link
Contributor

lol, no worries. They pay me to fix bugs... 😉

@bricelam
Copy link
Contributor

bricelam commented Jul 7, 2017

Looks like we anticipated this bug a while ago: #775

@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 Jul 7, 2017
@bricelam bricelam added good first issue This issue should be relatively straightforward to fix. and removed good first issue This issue should be relatively straightforward to fix. labels May 31, 2019
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

4 participants