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; }
    }
}



Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s