c# – How to add a “using” statement to the System.Data.Entity namespace

c# – How to add a “using” statement to the System.Data.Entity namespace

At the top of your file there should be couple lines, each starting with using and namespace after that keyword.

Add another one there, to the namespace mentioned in your tutorial:

using System.Data.Entity;

You can find more information about using on MSDN: using Directive (C# Reference)

Although your question is about the using statement, I believe you mean the using directive.

Using Directive

To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:

using System.Data.Entity;

namespace MyNamespace
{
    // Your code
}

Using directives are typically placed at the top of your file, but they can be placed at the top of your namespace as well.

namespace MyNamespace
{
    using System.Data.Entity;

    // Your code
}

For more information on using directives, see: using Directive (C# Reference)

A note about references: Before you can use System.Data.Entity, youll need to add a reference to the EntityFramework.dll. NuGet is a great tool for this, and can be invoked using the Visual Studio Package Manager.

Using Alias Directive

To create an alias for a namespace or a type. This is called a using alias directive:

using Project = PC.MyCompany.Project;

For more information on using alias directives, see: using Directive (C# Reference)

Using Statement

Provides a convenient syntax that ensures the correct use of IDisposable objects:

using (var font1 = new Font(Arial, 10.0f)) 
{
    byte charset = font1.GdiCharSet;
}

For more information on using statements, see: using Statement (C# Reference)

c# – How to add a “using” statement to the System.Data.Entity namespace

It is called using directive

Every class, enum and other elements used in programming in C# are contained inside a namespace.
To use these elements you need to refer to this namespace and this leads to a very long identifiers. The using directive instructs the compiler in which namespaces look for to find the definition of your elements.

You just need to add a line at the beginning of your class file stating the namespace that you intend to use in the remainder of your file.
For example, if you want to use a SqlConnection in your application, without the using directive you should write:

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(.....)

instead, adding

using System.Data.SqlClient;

you could write simply

SqlConnection con = new SqlConnection(.....)

Leave a Reply

Your email address will not be published.