locked
EF Core reverse engineering database bug? RRS feed

  • Question

  • User-1826179050 posted

    I am testing with VS 2019 Pro with an ASP.NET Core WebApp (MVC) project. The project name is NET-Core-WebApp. I used the following in the NuGet Package Manager Console:

    Scaffold-DbContext "Server=TSVR; Database=Dev-Test-01; Trusted_Connection=True; MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer -Tables Customers, CustomerTypes, Invoices -OutputDir Models

    This created my model classes, but I ended up with a bunch off errors - missing {, namespaces cannot have member, blah, blah, blah. In general, it seemed to not be happy about the namespaces used at the top of the model classes it created. I looked at the namespace being used by one of the model classes created initially in the project by the template. Here's the difference. The scaffolder created this namespace:

    namespace NET-Core-WebApp.Models

    whereas the template created model used this namespace:

    namespace NET_Core_WebApp.Models

    The scaffolder used the project name exactly - whereas the template replaced the '-' with '_' in the namespace. Once I did the same - all of these errors went away.

    Wednesday, July 31, 2019 3:39 PM

All replies

  • User1520731567 posted

    Hi ameador1,

    The scaffolder used the project name exactly - whereas the template replaced the '-' with '_' in the namespace. Once I did the same - all of these errors went away.

    Not a bug.

    Minus is a operators,it can't be used in name.

    Leading underscores is a convention.

    More details,you could refer to c# syntax rules for variable and property names.

    Best Regards.

    Yuki Tao

    Thursday, August 1, 2019 2:11 AM
  • User-1826179050 posted

    I think maybe you didn't understand my point. When I created my project - I named it NET-Core-WebApp (using dashes). Visual Studio had no issue with this name - it didn't say "Don't use dashes in the project name". When Visual Studio created the files and code in my base project, it also added a Model. In this model it created a namespace NET_Core_WebApp.Models (with underscores). Visual Studio changed the dashes into underscores when it created this namespace - as you say correctly as dashes are not supposed to be in the namespace. BUT - when I use the Scaffold-DbContext command in the package manager console to reverse engineer some tables in my database, Scaffold-DbContext DID NOT replace the dashes with underscores when it created the namespaces for the new Models. So, the namespace code generated by the Scaffold-DbContext is wrong a causes errors. I have to manually go back and change the dashes to underscores myself to remove the errors created by it since it is not replacing the dashes itself.

    This is AT LEAST and inconsistency between parts of the system as Visual Studio DID replace the dashes with underscores in the namespaces when it created the product. It doesn't make sense that Scaffold-DbContext then does not do the same. This adds confusion and inconsistency  - a logic error then...

    Thursday, August 1, 2019 2:42 AM
  • User1520731567 posted

    Hi ameador1,

    It doesn't make sense that Scaffold-DbContext then does not do the same. This adds confusion and inconsistency  - a logic error then...

    It seems that it is indeed a logical issue, but there should be a solution.

    I suggest you could modify your namespace to keep them consistent manually.

    Here is the similar issue in Github,you could refer to:

    https://github.com/aspnet/EntityFrameworkCore/issues/6369

    Best Regards.

    Yuki Tao

    Friday, August 2, 2019 5:43 AM
  • User-1826179050 posted

    Yeah, I noted that in my original post - that's what I did, manually renamed them. The errors that occurred did not clearly indicate that dashes in the namespace were the problem - and I did not expect the scaffolder to create code and/or namespaces that were invalid. So I was very confused for a bit. Until I decided to compare the model classes created by the scaffolder to the one created by Visual Studio. And, at quick glance I didn't notice the dashes were converted to underscores by Visual Studio. Once I noticed that, and I changed the dashes to underscore it worked - as I stated in more original post.

    The point is that this logic error created code that will not run and causes a person to have to research the issue and figure out what is going on and to then correct it manually. This is not good.

    Friday, August 2, 2019 12:44 PM
  • User1120430333 posted

    namespace NET-Core-WebApp.Models
    whereas the template created model used this namespace:
    namespace NET_Core_WebApp.Models

    Both namespace formats are legal. VS did it one way and the template did it another way. Now you know.

    Friday, August 2, 2019 6:28 PM
  • User-1826179050 posted

    No, both are NOT legal - that is the point. The namespace created by Scaffold-DbContext (namespace NET-Core-WebApp.Models) fails.

    Visual Studio's template created the one with underscores - which IS valid and does not fail.

    Thus my point that there is an inconsistent logic error between these two methods.

    Friday, August 2, 2019 6:44 PM
  • User1120430333 posted

    My mistake. I have never had the problem before with EF core scaffolding it always worked when creating the Model. 

    Saturday, August 3, 2019 1:40 AM