C#. Entity. CodeFirst_Example

Source Metanit

CodeFirst approach allows creating entities in DB from code…

lets start new console app and create User class

Now SolutionExplorer > Manage NuGetPackages for Solution > type Entity in search box and Install it to your project

Добавление Entity Framework через NuGet

Установка Entity Framework

Add using System.Data.Entity if not added before…

Now lets create UserContextClass like this

Now go to SolutionExplorer and add ConnectionString

 How to get connection string?

Tools > SQLServerObjectExplorer > Add SQL Server if needed > RightClick on Server and Choose Properties > Connection string will be the first in opened Panel

it will be something like this

you need to add

then it will be

so in App.config should be something like this

this also be Ok, changed Data Source=DESKTOP-T9U6IEP to (local)

and this. changed (local) to .

Now go to main and type like this – this code will create DB if needed and will add tables and fill records, and remove records if needed – see example…

 

 

This entry was posted in C#. Bookmark the permalink.

Leave a Reply