1

If I add a new migration (EF code-first), the structure of my new class/db migration looks a little something like:

namespace MySolutionNamespace.MyProjectNamespace
{
    using System.Data.Entity.Migrations;

    public partial class MyMigration : DbMigration
    {
        //....
    }
}

Is the reason for the using statements being rendered within the namespace documented anywhere?

  • I believe it's just a template which generates the code. Few of the code consistency tools like Stylecop recommend to define the using inside namespace; so it can be one of the reason. however, having using inside namespace and outside namespace does not matter. – user1672994 May 01 '18 at 15:39
  • @user1672994 `"however, having using inside namespace and outside namespace does not matter."` that is [completely false](https://stackoverflow.com/questions/125319/should-using-directives-be-inside-or-outside-the-namespace) – maccettura May 01 '18 at 15:44
  • In the code above, the using statement is outside the namespace scope. Is that what EF generates? – iCode May 01 '18 at 16:04
  • @maccettura - Thanks. Noted that. – user1672994 May 01 '18 at 16:19
  • @maccettura You're correct of course, but I find all of the examples of differences extremely contrived. I would generally say: "Having using directives inside vs. outside the namespace does not matter *unless you're doing some crazy crap with namespaces.*" – BJ Myers May 01 '18 at 16:21
  • @iCode - don't know how that happened, edited the OP, thanks. –  May 02 '18 at 08:06
  • Possible duplicate of [Should 'using' directives be inside or outside the namespace?](https://stackoverflow.com/questions/125319/should-using-directives-be-inside-or-outside-the-namespace) – phuzi May 02 '18 at 08:09
  • 1
    Ah, seems somebody edited my question to move the using directive, which totally defeats the purpose of the question. –  May 02 '18 at 08:16

1 Answers1

2

There are several good posts/answers on SO on this topic. Here’s on explaining how you could potentially run into issues when scoping namespaces at the top of the file: https://stackoverflow.com/a/151560/1617161

Here’s another discussing potential issues: https://stackoverflow.com/a/1342873/1617161

I’ve always placed them at the top of the file, which seems safe if your namespaces and classes are very application specific; ie. Will not clash with other Dlls.

I suppose specific to Entity Framework, it would seem that they’re trying to prevent collisions with the classes in the System.Data.Entity.Migrations namespace. Placing inside the namespace lessens the chance of breaking someone’s code that’s has a class that’s named the same as a class in the System.Data.Entity.Migrations namespace.

iCode
  • 1,254
  • 1
  • 13
  • 16