Giter Site home page Giter Site logo

mortalwind / codefirstsample Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 38 KB

This is very simple sample for Code-First approach EF Core in Docker.

License: GNU General Public License v3.0

C# 95.87% Dockerfile 4.13%
docker automapper code-first code-first-migration core dependency-injection ef

codefirstsample's Introduction

CodeFirstSample

Hello everyone,

This project is a sample of the Code-First approach for database creation strategy.

Code-First is mainly useful in Domain Driven Design.

About this project

You can imagine that you've got a web application that lists your books and their authors. Maybe you're a bookseller :)

And this application lists your books with prices. You can get about of book details.

How does it work?

This project is very simple. It shows how can use EF Core Code-First approach in Docker container.

Requirements:

  • You should have Docker Desktop on your PC.

  • Visual Studio Code, Visual Studio, or a similar application must be installed on your PC.

  • You better know Git commands.

  • You should know C#, you better know Dockerfile and docker-compose commands.

If you don't know Docker containers and their command, it's no problem, I'll explain below.

Let's start!

Open your Command Prompt,Terminal or Powershell then go to your projects directory. Type git clone https://github.com/mortalwind/codefirstsample.git code, press Enter. Wait while Git is cloning the project in your project directory.

And you can edit it with VS Code using this code code -r CodeFirstSample or you can open it in your editor directly.

After this moment, I'll continue with VS Code.

Run the project

If it's closed then you can open a new terminal in VS Code. You can do that clicking Terminal menu and click New Terminal.

Next, ensure Docker Desktop is running. Run this command docker-compose build . This command runs your docker-compose.yaml file where in the project root folder.

After finish the build, you can run your container like that docker-compose up -d .

Open your Docker Desktop and goto Containers tab. You can find a container that is named "codefirstsample" which contains two images inside. "CodeFirstSampleAPI" and "mssqldb" images will be created. And both of them are running.

Initialize the database

There is a little issue with database migrations. Because it's about container lifecycle. Our API project runs up quickly. But our mssql database image needs some installation and configuration processes. And, when we would try to migrate the database when API's startup we would get errors. Because our MSSQL database is not ready yet.

We will wait until the database image is ready. You can view docker image logs by click on mssldb image.

Our API project port is 55099. Open your browser and paste this http://localhost:55099/api/books . You will get an error about that login failed as 'sa' for 'bookstore' database. And open new tab, paste this http://localhost:55099/api/initialize then go. A few moments later it writes "Database migration is finished.". After that go back your books page and refresh page. Note: I'm sure that Postman will be better than your browser for these actions.

Yeay! The error is disappered. But there is no data. I told you this is a simple project:) We will add books and authors manually.

Let's take a look at the project

Structure:

  • Abstractions: It contains interfaces and other abstract classes.
  • Controllers: It contains Controllers.
  • Data: It contains database configurations.
  • Models: It contains DTOs and database models
  • Extensions: It contains extension methods.
  • Profiles: It contains Automapper profiles.
  • Repositories: It contains generic repository pattern
  • Services: It containes services what business between api and database
  • docker-compose.yml: the configuration file for dockerize process.

codefirstsample's People

Contributors

mortalwind avatar

Watchers

 avatar

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.