{"id":676,"date":"2023-10-23T08:39:12","date_gmt":"2023-10-23T14:39:12","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=676"},"modified":"2023-10-23T08:39:12","modified_gmt":"2023-10-23T14:39:12","slug":"one-to-one-relationship-in-dbcontext","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/one-to-one-relationship-in-dbcontext\/","title":{"rendered":"One to One Relationship in DbContext"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">using System;\nusing System.Collections.Generic;\nusing System.ComponentModel.DataAnnotations;\nusing System.ComponentModel.DataAnnotations.Schema;\nusing System.Linq;\nusing Microsoft.Data.SqlClient;\nusing Microsoft.EntityFrameworkCore;\n\npublic class Program\n{\n    public static string ConnectionString = FiddleHelper.GetConnectionStringSqlServer();\n\n    public static void Main()\n    {\n        using (var context = new MyDbContext())\n        {\n            \/\/ Create the database if it doesn't exist\n            \/\/ Ensure creation of the database\n            context.Database.EnsureCreated();\n\n            \/\/ Seed the data\n            \/\/ the content is seeded\n            SeedData(context);\n\n            \/\/ Query the data\n            var results = context.Parents\n                .Include(p => p.Child1)\n                .Include(p => p.Child2)\n                .ToList();\n\n            \/\/ Output the results\n            FiddleHelper.WriteTable(results);\n        }\n    }\n\n    \/\/ Creating the Dbcontext\n    public class MyDbContext : DbContext\n    {\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n        {\n            optionsBuilder.UseSqlServer(ConnectionString);\n        }\n\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\n        {\n            modelBuilder.Entity&lt;Parent>()\n                .HasOne(p => p.Child1)\n                .WithOne(c => c.Parent)\n                .HasForeignKey&lt;Child1>(c => c.ParentId);\n\n            modelBuilder.Entity&lt;Parent>()\n                .HasOne(p => p.Child2)\n                .WithOne(c => c.Parent)\n                .HasForeignKey&lt;Child2>(c => c.ParentId);\n        }\n\n        public DbSet&lt;Parent> Parents { get; set; }\n        public DbSet&lt;Child1> Children1 { get; set; }\n        public DbSet&lt;Child2> Children2 { get; set; }\n    }\n\n    \/\/ Creating Models\n    public class Parent\n    {\n        [Key]\n        public int Id { get; set; }\n\n        public string Name { get; set; }\n\n        public Child1 Child1 { get; set; }\n        public Child2 Child2 { get; set; }\n    }\n\n    public class Child1\n    {\n        [Key]\n        public int Id { get; set; }\n\n        public string Name { get; set; }\n\n        public int ParentId { get; set; }\n        public Parent Parent { get; set; }\n    }\n\n    public class Child2\n    {\n        [Key]\n        public int Id { get; set; }\n\n        public string Name { get; set; }\n\n        public int ParentId { get; set; }\n        public Parent Parent { get; set; }\n    }\n\n    \/\/ Seeding data\n    public static void SeedData(MyDbContext context)\n    {\n        var parent1 = new Parent { Name = \"Parent 1\" };\n        var parent2 = new Parent { Name = \"Parent 2\" };\n\n        var child1_1 = new Child1 { Name = \"Child 1-1\", Parent = parent1 };\n        var child1_2 = new Child1 { Name = \"Child 1-2\", Parent = parent2 };\n\n        var child2_1 = new Child2 { Name = \"Child 2-1\", Parent = parent1 };\n        var child2_2 = new Child2 { Name = \"Child 2-2\", Parent = parent2 };\n\n        context.Parents.AddRange(parent1, parent2);\n        context.Children1.AddRange(child1_1, child1_2);\n        context.Children2.AddRange(child2_1, child2_2);\n\n        context.SaveChanges();\n    }\n}<\/code><\/pre><\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":259,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[39,41,51,52,40,111],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/676"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/676\/revisions\/677"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/259"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}