How to add a “using” statement to the System.Data.

2019-07-10 19:51发布

问题:

I am new to Visual Studio and C# in general. I'm following a tutorial trying to learn the language (tutorial here).

One of the steps is to add a “using” statement to the System.Data.Entity namespace at the top of the class file (in order to reference the DbContext and DbSet classes). This can be found under step 3.

I'm hoping someone can help me out with this section. I've searched on here and Google for an answer, but since I'm new to the language I can't seem to find the correct answer. Any help would be greatly appreciated!

回答1:

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)



回答2:

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, you'll 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)



回答3:

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(.....)


回答4:

You can press Ctrl + . (dot) in Visual Studio and using statement will be added automatically.