soutsjjw
8/23/2018 - 3:52 PM

EF6 -> One-to-One HasOptional then WithOptionalDependent,Student會另外增加Contact_Id欄位並且為外鍵

EF6 -> One-to-One HasOptional then WithOptionalDependent,Student會另外增加Contact_Id欄位並且為外鍵

namespace EntityFramework6.Entity
{
    public class Student
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public virtual StudentContact Contact { get; set; }
    }
	
    public class StudentContact
    {
        public long Id { get; set; }
        public string ContactNumber { get; set; }
        public virtual Student Student { get; set; }
    }
}

namespace EntityFramework6.Map
{
    public class StudentMap : EntityTypeConfiguration<Student>
    {
        public StudentMap()
        {
            // table
            ToTable("Students");

            // key
            HasKey(t => t.Id);

            // fields
            Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // relationship
            HasOptional(x => x.Contact).WithOptionalDependent(l => l.Student);
        }
    }
	
    public class StudentContactMap : EntityTypeConfiguration<StudentContact>
    {
        public StudentContactMap()
        {
            // table
            ToTable("StudentContacts");

            // key
            HasKey(x => x.Id);

            // fields
            Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }
    }
}

namespace EntityFramework6
{
    public class EfDbContext : DbContext
    {
        public EfDbContext() : base("name=ConnectionString")
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<EfDbContext>());
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<StudentContact> StudentContacts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
                .Where(type => !string.IsNullOrEmpty(type.Namespace))
                .Where(type => type.BaseType != null && type.BaseType.IsGenericType
                                                     && type.BaseType.GetGenericTypeDefinition() ==
                                                     typeof(EntityTypeConfiguration<>));

            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }

            base.OnModelCreating(modelBuilder);
        }
    }
	
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new EfDbContext())
            {
                ctx.Database.Log = Console.WriteLine;

                var student = new Student
                {
                    Name = "Jeffcky"
                };
                var student1 = new Student
                {
                    Name = "Jeffcky1",
                    Contact = new StudentContact
                    {
                        ContactNumber = "1234567890"
                    }
                };
                var student2 = new Student
                {
                    Name = "Jeffcky2",
                    Contact = new StudentContact
                    {
                        ContactNumber = "0987654321"
                    }
                };

                ctx.Students.Add(student);
                ctx.Students.Add(student1);
                ctx.Students.Add(student2);

                ctx.SaveChanges();
            }
            Console.ReadKey();
        }
    }
}