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; }
}
}
Discover more from Dhananjay Kumar
Subscribe to get the latest posts sent to your email.
One thought on “Video : One to one relationship between entities in Entity Framework Code First”