项目作者: henriq-toledo

项目描述 :
C# Class Project using Visual Studio Code and .NET Core 2.2
高级语言: C#
项目地址: git://github.com/henriq-toledo/c-sharp-class-project.git
创建时间: 2019-11-12T23:37:41Z
项目社区:https://github.com/henriq-toledo/c-sharp-class-project

开源协议:

下载


C# Class Project

Employees Register

This project will list, create, update and delete an employee.

Commands used to create the project:

  • mkdir c-sharp-class-project
  • cd c-sharp-class-project
  • git init
  • include the .gitignore, .gitattributes and the README.md files
  • git config user.name “My User Name”
  • git config user.email myemail@mail.com
  • git config —list
  • mkdir src
  • cd src
  • mkdir CSharpClassProject
  • cd CSharpClassProject
  • dotnet new sln
  • mkdir CSharpClassProject.EmployeesRegister
  • cd CSharpClassProject.EmployeesRegister
  • dotnet new console
  • cd ..
  • dotnet sln add .\CSharpClassProject.EmployeesRegister\CSharpClassProject.EmployeesRegister.csproj
  • cd ..
  • cd ..
  • git add .
  • git status
  • git commit -m “Initial commit.”
  • git log
  • git remote add origin \
  • git push -u origin master

EF CORE

To install the Entity Framework Core, open the project folder in the terminal and run the command below:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

To execute the dotnet ef command, install the package below:

dotnet add package Microsoft.EntityFrameworkCore.Design


dotnet add package Microsoft.EntityFrameworkCore.Tools


dotnet tool install —global dotnet-ef —version 2.2.6

Create the Employee abstract class inside the Classes\Entities folder:

  1. namespace CSharpClassProject.EfCore.Classes.Entities
  2. {
  3. public abstract class Employee
  4. {
  5. public int Id { get; set; }
  6. public string Name { get; set; }
  7. public string CompanyName { get; set; }
  8. }
  9. }

Create the Developer class inheriting from the Employee class inside the Classes\Entities folder:

  1. namespace CSharpClassProject.EfCore.Classes.Entities
  2. {
  3. public class Developer : Employee
  4. {
  5. }
  6. }

Create the IContext interface in the Interfaces folder:

  1. using CSharpClassProject.EfCore.Classes.Entities;
  2. using Microsoft.EntityFrameworkCore;
  3. namespace CSharpClassProject.EfCore.Interfaces
  4. {
  5. public interface IContext
  6. {
  7. DbSet<Developer> Developers { get; set; }
  8. }
  9. }

Then in the Classes\Entities folder, create the Context class inheriting from the DbContext class, responsible to the database context, and implements the IContext interface created above:

  1. using CSharpClassProject.EfCore.Interfaces;
  2. using Microsoft.EntityFrameworkCore;
  3. namespace CSharpClassProject.EfCore.Classes.Entities
  4. {
  5. public class Context : DbContext, IContext
  6. {
  7. public DbSet<Developer> Developers { get; set; }
  8. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  9. {
  10. // Configures the database connection string
  11. optionsBuilder.UseSqlServer("<connection string>");
  12. }
  13. }
  14. }

Now, build the project and then create the initial migration:

dotnet ef migrations add InitialMigration

A Migration folder will be created with tree files (the 20200523165930 is the time when you ran the dotnet ef migrations command):

  • 20200523165930_InitialMigration.cs: contains the migrations code to be executed in the database
  • 20200523165930_InitialMigration.Designer.cs: contains the migration id and detail code to be executed in the database
  • ContextModelSnapshot.cs: contains the entire database snapshot

Then, run the command below to update the database:

dotnet ef database update

To see the entire command that will be executed in the update, add the —verbose parameter:

dotnet ef database update —verbose

To revert some change, execute the comand below:

dotnet ef database update \

References:

https://docs.microsoft.com/en-us/ef/core/get-started/install/


https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings


https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet