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?

8
  • 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. May 1, 2018 at 15:39
  • @user1672994 "however, having using inside namespace and outside namespace does not matter." that is completely false
    – maccettura
    May 1, 2018 at 15:44
  • In the code above, the using statement is outside the namespace scope. Is that what EF generates?
    – iCode
    May 1, 2018 at 16:04
  • @maccettura - Thanks. Noted that. May 1, 2018 at 16:19
  • 1
    Ah, seems somebody edited my question to move the using directive, which totally defeats the purpose of the question.
    – user1017882
    May 2, 2018 at 8:16

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.