GithubHelp home page GithubHelp logo

kolosovpetro / codefirstentityframeworkcore Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 371 KB

Simple example of Entity Framework Core Code-First approach.

C# 98.88% TSQL 1.12%
entity-framework entity-framework-core efcore ef-core postgresql postgresql-database postgres-database sqlserver mssqlserver mssql-database

codefirstentityframeworkcore's Introduction

Code First with Entity Framework

Simple example of Entity Framework Core Code-First approach.

Data base schema

DbSchema

Required packages and settings

Nuget Packages:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.SqlServer
  • Npgsql.EntityFrameworkCore.PostgreSQL

Settings:

  • Environmental variable: SQLSERVER_MOVIES_LOCAL_CONNSTR
  • Environmental variable: POSTGRES_MOVIES_LOCAL_CONNSTR

Entity Framework CLI commands

Postgres:

  • dotnet-ef migrations add PostgreSqlInitialMigration --project CodeFirst.DataAccess --context PostgresDbContext
  • dotnet-ef database update --project CodeFirst.DataAccess --context PostgresDbContext
  • dotnet-ef database drop --project CodeFirst.DataAccess --context PostgresDbContext

MS SQL Server:

  • dotnet-ef migrations add SqlServerInitialMigration --project CodeFirst.DataAccess --context SqlServerDbContext
  • dotnet-ef database update --project CodeFirst.DataAccess --context SqlServerDbContext
  • dotnet-ef database drop --project CodeFirst.DataAccess --context PostgresDbContext

How to code first?

  • Create a separate project Models, where data models are stored.

  • Create a separate project DataAccess, that is responsible for interaction with database.

  • Load packages to DataAccess project. See section Required packages.

  • Set proper environmental variable for connection string for SQL Server database. For localhost it is:

    "Data Source=DESKTOP-RN0NICT;Initial Catalog=MoviesCodeFirst;Integrated Security=true;"

  • Set proper environmental variable for connection string for Postgre SQL database. For localhost it is:

    "Server=localhost;User Id=postgres;Password=postgres;Database=MoviesCodeFirst;"

  • Build a relations between models, for instance, one-to-one, one-to-many, many-to-many according to the database diagram. Refer to

  • Create proper configurations of models, using Fluent API. Configuration should implement IEntityTypeConfiguration<T> interface from Entity Framework namespace

  • Create BaseContext inherrit it from DbContext in Entity Framework namespace, add related sets BDSet<T>, where T -- models.

  • Create SqlServerContext, inherrit it from BaseContext and override methods OnConfiguring and OnModelCreating.

  • Create PostgreSqlContext, inherrit it from BaseContext and override methods OnConfiguring and OnModelCreating.

  • Create migration for SqlServerContext, use CLI command:

    dotnet-ef migrations add SqlServerInitialMigration --project CodeFirst.DataAccess --context SqlServerDbContext

  • Create migration for PostgreSqlContext, use CLI command:

    dotnet-ef migrations add PostgreSqlInitialMigration --project CodeFirst.DataAccess --context PostgresDbContext

  • Update SQL Server database, use CLI command:

    dotnet-ef database update --project CodeFirst.DataAccess --context SqlServerDbContext

  • Update Postgre SQL database, use CLI command:

    dotnet-ef database update --project CodeFirst.DataAccess --context PostgresDbContext

  • Write unit tests for SqlServerDbContext

  • Write unit tests for PostgresDbContext

  • Create Generic repository interface

  • Implement Generic repository interface

  • Test Generic Repositories

Notes

To get MS SQL connection string proceed:

  • Go to visual studio - Server explorer - Data connections
  • Right click on data connections - Add new connection
  • Type there your server name
  • Click add connection
  • Then in properties of such connection you'd get connection string

codefirstentityframeworkcore's People

Contributors

kolosovpetro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

codefirstentityframeworkcore's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.