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

EnsureCreated not working when exist a Database Diagrams on SQL Server #14890

Closed
m4ss1m0g opened this issue Mar 1, 2019 · 9 comments
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported 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-enhancement
Milestone

Comments

@m4ss1m0g
Copy link
Contributor

m4ss1m0g commented Mar 1, 2019

Description

When use SQLServer with database diagrams, the EnsureCreated command fail without any error.

Steps to reproduce

Define a simple DbContext with a single Entity

public class Book 
{
    public int Id {get; set;}

    public string Title {get; set; }
}

public class AppDbContext : DbContext
{
    public DbSet<Book> Books { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        const string cs = @"Server=.\sqlexpress;Database=EFCore;Trusted_Connection=true";
        optionsBuilder.UseSqlServer(cs);
    }
}

Open SSMS and create a new database diagrams (is sufficient to create the table sysdiagrams) and create an empty diagram

Launch the EnsureCreated command

using (var db = new AppDbContext())
{
    db.Database.EnsureCreated();
}

Check the SSMS and No Tables are created

Workaround

DROP table sysdiagrams under System Tables node

Further technical details

EF Core version: 2.2.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.9.7

@ajcvickers
Copy link
Contributor

@m4ss1m0g Curious: what's your intended workflow around using EnsureCreated? For example, if you need to modify the database after the initial creation, how do you intend to do that?

@m4ss1m0g
Copy link
Contributor Author

m4ss1m0g commented Mar 1, 2019

@m4ss1m0g Curious: what's your intended workflow around using EnsureCreated? For example, if you need to modify the database after the initial creation, how do you intend to do that?

I already know EF6 and the migration tools, I use EnsureCreate only for dev/testing.
I learning EF Core and some times I use the EnsureCreate to create the DB, then with the DB Diagram I check if the tables and relations are correct.

@ajcvickers
Copy link
Contributor

@m4ss1m0g So do you ever call EnsureDeleted?

@m4ss1m0g
Copy link
Contributor Author

m4ss1m0g commented Mar 3, 2019

The problem is on EnsureCreated AND the sysdiagrams table.

  1. Create a new Database with SSMS
  2. On the new database right click on Database Diagram node and select new database diagram
  3. Click yes when asking for create the support object, this create a new sysdiagrams table under system tables
  4. Launch the EnsureCreated
  5. Check SSMS and verify that NO tables was created.
  6. Delete the sysdiagrams table
  7. Launch the EnsureCreated
  8. Check SSMS and verify that tables WAS created.

@ajcvickers
Copy link
Contributor

ajcvickers commented Mar 3, 2019

@m4ss1m0g I understand what is happening; that's not the point of this conversion. The current behavior is by-design; it's possible we could change that, but to do so the end-to-end experience needs to make sense. Since usually EnsureCreated is used together with EnsureDeleted, and EnsureDeleted will delete the entire database, including system tables. What we're trying to understand is whether or not it makes sense to allow EnsureCreated to ignore something that EnsureDeleted will remove. So, to move forward with this, I'm asking for more information about the bigger picture in which you are using EnsureCreated.

@m4ss1m0g
Copy link
Contributor Author

m4ss1m0g commented Mar 4, 2019

@ajcvickers I did not know EnsureDeleted ! In my workflow I deleted the tables by DROP TABLE command. Now has more sense.
However I noticed that EnsureDeleted remove ALL tables, also tables from other schemas; I think a warning message or a better documentation would be much appreciated.
Thanks

@ajcvickers
Copy link
Contributor

Note from triage: it makes sense to ignore system tables when considering whether or not the database is "empty", even though EnsureDeleted deletes the entire database, including system tables.

@ajcvickers ajcvickers added this to the Backlog milestone Mar 4, 2019
@ajcvickers ajcvickers added type-enhancement help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. labels Mar 4, 2019
ErikEJ added a commit to ErikEJ/EntityFramework that referenced this issue Apr 24, 2019
@ErikEJ
Copy link
Contributor

ErikEJ commented May 6, 2019

Should the milestone not be updated?

@smitpatel smitpatel self-assigned this May 6, 2019
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 6, 2019
@smitpatel smitpatel modified the milestones: Backlog, 3.0.0 May 6, 2019
@smitpatel
Copy link
Contributor

Updated tags.

@bricelam bricelam added the good first issue This issue should be relatively straightforward to fix. label May 31, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview6 Jun 5, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview6, 3.0.0 Nov 11, 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. customer-reported 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-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants