Video : One to one relationship between entities in Entity Framework Code First

Container.cs



namespace EFCodeFirstDemo.EFLib
{
   public class Container
    {
        public int ID { get; set; }
        public string ContainerName { get; set; }

        public virtual  Product product { get; set; }

    }
}


Product.cs


using System.ComponentModel.DataAnnotations;

namespace EFCodeFirstDemo.EFLib
{
   public class Product
    {
   
      // [ForeignKey("Container")]
        public int ID { get; set; }
        public string  ProductName { get; set; }

        public double  ProductPrice { get; set; }

        [Required]
        public virtual Container container { get; set; }
    }
}



EFContext.cs


using System.Data.Entity;

namespace EFCodeFirstDemo.EFLib
{
   public class EFContext : DbContext
    {
       public EFContext()
       {
           Database.SetInitializer<EFContext>(new DropCreateDatabaseIfModelChanges<EFContext>());
       }

       public DbSet<Product> Products { get; set; }
       public DbSet<Container> Containers { get; set; }
    }
}



One response to “Video : One to one relationship between entities in Entity Framework Code First”

  1. […] Video : One to one relationship between entities in Entity Framework Code First (DJ) […]

Leave a comment

Create a website or blog at WordPress.com