2021

EntityFramework FluentAPI & DataAnnotations

Two CRUD projects demonstrating EF Core DataAnnotations, FluentAPI, Console CRUD and WinForms GUI CRUD.

EntityFramework - FluentAPI and DataAnnotations

EntityFramework - FluentAPI and DataAnnotations

I document here my two CRUD projects that I made as a class project. At first I learned the fundemental logic of .NET Core and moved onto the first project.I started writing the code by the right EF work flows, first the Console output project which where you can Create a post,Create a comment,Create a user(Menu 8),View,Edit and Delete.and the second project can Create,View and edit columns as a WinForm GUI.

DataAnnotations

● The Class’s

The Posts Class is now being written and I am using here* DataAnnotations method so for example Post_Id will be the PrimaryKey in the Table so I will use [Key] one line above. After giving each column its type I open up NuGet PMC and add-migration,update-database.

DataAnnotation

FluentAPI

● Fluent Class’s

After using Data Annotations I created two new class’s both with the same columns and values as in Posts and Comments class’s only now I remove all [annotations].
The new Class called FluentPostConfig will inherit from IEntityTypeConfiguration interface using .Metadata.Builders And now I use the Configure method which contains a generic EntityTypeBuilder object and now I can start using the Fluent code,for example like before I want Post_Id to be my PK so I can write now objectName.HasKey(p => p.Post_Id);

fluentAPI
the lambada expression will indicate that the Post_Id column is the PrimaryKey of the table.

● DBContext Class The Data-Base Context class is connecting to SQL via the OnConfiguring method which contains a DbContextOptionsBuilder object where I can connect into the Data-Base
objectName.UseSqlServer(“Server=localhost\SQLEXPRESS;Database=Data-Base;Trusted_Connection=True;MultipleActiveResultSets=True;”);

DataAnnotationContext

The Data-Base Context class uses the same DataBase Set properties only now I wont be using the OnConfiguring method I will use the OnModelCreating method which contains a ModelBuilder object which will apply configurations to the Data-Base so in the code I am using the FluentConfig class’s that I made before because I want to stay as orginaized as I can.

FAPICONTEXT

Second Project - WinForms CRUD

0

1

2

3

4

5

6

7

First Project - Console CRUD

0

2

3

6

8

9

10

12

13

16

18

20

21

22

Screenshot

EntityFramework Projects
Explore more projects