efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Reverse Engineer: Better support for namespaces

Open roji opened this issue 10 years ago • 40 comments

Table schemas don't appear to make it into scaffolded entity code in any way; If I have two tables named Users in schemas A and B, the reverse engineering process will simply created Users and Users1 inside the reverse-engineering namespace. This don't seem to be a very satisfactory result.

I think the expected result is probably to map non-default database schemas (i.e. non-dbo in SqlServer) to namespace underneath the reveng namespace. So, assuming we're reverse-engineering to namespace Models, dbo.Users would get scaffolded to Users.cs, while schema2.Users would get scaffolded to Models.Schema2.Users.

roji avatar Dec 06 '15 12:12 roji

You should get either the [Table] attribute on the POCO class or the equivalent fluent API ToTable() in the context OnModelCreating() method saying what the underlying schema/table name are. Please let me know if you're not seeing this.

I know we've discussed before having the schema in the namespace and decided not to. I'm not sure if this was just to stay consistent with what EF6 did or whether there are other reasons. @rowanmiller do you remember?

lajones avatar Dec 07 '15 17:12 lajones

We should re-discuss this, it was one of the biggest complaints about reverse engineer in EF6.

rowanmiller avatar Dec 07 '15 18:12 rowanmiller

Discussed and agreed that this would be good. We could detect if you have multiple schemas and start doing sub-folders/sub-namespaces automatically... or we could just have a flag that you supply.

For the moment, we are just ok with the trivial algorithm we have. If we require the flag when we implement this, then we should write a warning telling folks about the flag.

rowanmiller avatar Dec 08 '15 20:12 rowanmiller

Note: the "DB schema translates to C# namespace" suggestion was also made as part of #3861 (and many other places I'm sure).

lajones avatar Dec 11 '15 18:12 lajones

Hope this get developed soon :) It will make it lot easier when generating contexts with different schemas.

connde avatar Feb 02 '16 13:02 connde

@rowanmiller Hi,

In addition to @roji's, another (more general I guess) aproach also making use of a parameter -ContextNamespace, cause even if they may be stored in subfolders, it would be usefull to override the default namespace in the command for general purposes. Ex:

I want to scaffold all tables into Model\Design : This makes much sense, since I want to keep Model root folder to adaptations and extensions to the scaffolded Model (wich EF also suggests by design while defining classes as partial).

Scaffold-DbContext (...) -OutputDir Model\Design will generate on namespace <project>.Models.Design. ...Too Bad :(

the goal is not only to have a parameter we can use to place the files into a folder (-OutputDir), but also another parameter to define the context namespace. In this case:

Scaffold-DbContext (...) -OutputDir Model\Design -ContextNamespace <project>.Models...

This would output in Models\Design files with namespace '.Models'.

@roji's scenario would be accomplished with something like this:

Scaffold-DbContext (...) -OutputDir Model\Schema -Schema <schema> -ContextNamespace <project>.Model.<schema> in the core, iterating thru the schemas ....

olavorn avatar Jul 05 '16 02:07 olavorn

@olavorn your suggestion seems like a separate issue - allowing users to control the namespace separately from the OutputDir isn't the same as asking for schemas to be automatically translated into C# namespaces (even if it your feature proposal would provide a clunky manual workaround). I'd open a separate issue on this.

roji avatar Jul 05 '16 07:07 roji

@roji I agree with you, but before that, seems to me a good idea to exaust this a little bit here; As for the clunky workaround, the last example I put was more like an implementation suggestion for your feature, but it could alse be wrapped all up in a single flag like :

Scaffold-DbContext (...) -OutputDir Models -ContextNamespace <project>.Models -SchemaHierarchy [single|subfolders]

Like that, impact on the current feature may be minimun..

olavorn avatar Jul 05 '16 12:07 olavorn

I totally agree with @olavorn's idea. Now I only have 2 choices:

  • Change my partial classes' namespace to the one of my Scaffolded models (I want the other way)
  • mix all my partial classes together (Scaffolded and non-Scaffolded)

I like none of these choices, I would much prefer be able to specify the namespace of my Scaffolded models so I could store my partial classes in separate folders

jmevel avatar Oct 24 '16 08:10 jmevel

i would also like to be able to define my namespace when scaffolding the db, has there been any traction with this?

adayIvey avatar Dec 21 '16 16:12 adayIvey

@aday12345 This issue is in our backlog milestone. That means we don't have immediate plans to work on it. I am however clearing up the milestone to re-discuss the priority. If anyone wanted to make a contribution here, we would be happy to consider it too.

divega avatar Dec 21 '16 22:12 divega

Note: when designing/implementing this, also consider the issues raised in #8852

ajcvickers avatar Jun 19 '17 19:06 ajcvickers

just wondering if there has been any progress on defining the name space when scaffolding an existing db

adayIvey avatar Oct 03 '17 14:10 adayIvey

@aday12345 We discussed and even though we are doing some work in the background for reverse engineering, unfortunately we don't think this specific issue fits in the schedule for 2.1.

ajcvickers avatar Oct 04 '17 19:10 ajcvickers

ok, thanks.

Is the any usage documentation for scaffolding the list all the options and switch?

adayIvey avatar Oct 05 '17 12:10 adayIvey

@aday12345 - Use Get-Help Scaffold-DbContext in Package Manager console or dotnet ef dbcontext scaffold --help if you are using dotnet CLI

smitpatel avatar Oct 05 '17 16:10 smitpatel

@aday12345 Also https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

ajcvickers avatar Oct 05 '17 16:10 ajcvickers

thanks

adayIvey avatar Oct 05 '17 17:10 adayIvey

@bricelam Is there any development for a flag, or any other option, for having the schemas separates in the different folders?

Pets avatar Sep 13 '18 20:09 Pets

Seems a lot of people want this (myself included) any chance this will actually make it into EFCore soon?

redoz avatar Sep 18 '18 13:09 redoz