Code Monkey home page Code Monkey logo

005_ef's Introduction

005_EF

One-to-One Relationship in Entity Framework

This repository presents a code example demonstrating a "one-to-one" relationship in Entity Framework.

Defining the "One-to-One" Relationship

A "one-to-one" relationship in Entity Framework is a connection between two entities, where each entity from one data set (table) is associated with one and only one entity from another data set (table).

Code Structure

User

A class representing a user. Linked to the UserProfile entity through the UserProfile property.

namespace _005_EF
{
    internal class User
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public string? Password { get; set; }
        public UserProfile UserProfile { get; set; }
    }
}

UserProfile

A class representing a user profile. It is linked to the User entity through the User property.

namespace _005_EF
{
    internal class UserProfile
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public int Age { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
    }
}

Context

The Context class represents the Entity Framework database context. It includes data sets for the User and UserProfile entities and is configured to work with SQL Server.

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;

namespace _005_EF
{
    internal class Context : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<UserProfile> Profiles { get; set; }

        public Context()
        {
            Database.EnsureCreated();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var builder = new SqlConnectionStringBuilder
            {
                DataSource = ".",
                InitialCatalog = "OneToOneTest",
                IntegratedSecurity = true,
                TrustServerCertificate = true,
            };
            optionsBuilder.UseSqlServer(builder.ConnectionString);
        }
    }
}

Main Code Meaning

The provided code illustrates the establishment of a "one-to-one" relationship between the User and UserProfile entities. In this relationship, each user is associated with only one profile, defining its characteristics. This design allows for effective data storage related to users by utilizing a separate profile table, ultimately minimizing data redundancy.

005_ef's People

Contributors

nerses2001 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.