Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

.net

What is EF Core?

Fahad Farid

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
widget

Entity Framework core, the latest Entity Framework, is an Object/Relational Mapping framework that enables .NET developers to work with a databases that uses the .NET object. This, therefore, eliminates the need for a data access code.

EF currently works for the following database providers:

svg viewer

The model

A model is made up of classes and context objects that represent a session with the database and allows users to query and save data. In EF Core, a model is used to access data.

A model can be generated from an existing db (handcoded to match the db) or EF migrationskeeping the database schema in sync with the EF Core model by preserving data can be used to create a database from your model that changes as the model evolves.

Here is an example of an EF Model:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace Intro
{
    public class VideoContext : DbContext
    {
        public DbSet<Video> Videos { get; set; }
        public DbSet<Comment> Comments { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                @"Server=(localdb)\mssqllocaldb;Database=Video;Integrated Security=True");
        }
    }

    public class Video
    {
        public int VideoId { get; set; }
        public string Url { get; set; }
        public int Rating { get; set; }
        public List<Comment> Comments { get; set; }
    }

    public class Comment
    {
        public int CommentId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int VideoId { get; set; }
        public Video Video { get; set; }
    }
}

Querying

Instances of entity classes are retrieved from the database using Language Integrated Query (LINQ).

Here is an example of how to query:

using (var db = new VideoContext())
{
    var video = db.Video
        .Where(v => v.Rating > 4)
        .OrderBy(v => v.Url)
        .ToList();
}

Saving Data

Instances of the entity class can be used to create, delete, and modify data in the database.

Here is an example of how to save data:

using (var db = new VideoContext())
{
    var video = new video { Url = "http://educative.io/vid" };
    db.Videos.Add(video);
    db.SaveChanges();
}

RELATED TAGS

.net

CONTRIBUTOR

Fahad Farid
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring