日期:2014-05-18  浏览次数:21201 次

entity framework(EF)_code first复杂类型(Complex Types)问题
C# code

public class Address
    {
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string Addr { get; set; }
       public string Street { get; set; }
       public string City { get; set; }
       public string ZipCode { get; set; }
       public string Email { get; set; }
       public string Phone { get; set; }
       public string State { get; set; }
       public string Country { get; set; }
    }


C# code

public class Order
    {
       public int OrderId { get; set; }
       public string OrderNumber { get; set; }
       public DateTime OrderData{get;set;}
       public string DisposeDate { get; set; }

       public Address ShipAddress { get; set;}
       public Address BillAddress { get; set; }
}


C# code


  //然后我们modelbuilder
 //BillAddress
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.Addr).HasColumnName("BillAddr")
                .HasMaxLength(128);//A:注意这里
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.Street).HasColumnName("BillStreet")
                .HasMaxLength(128);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.City).HasColumnName("BillCity")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.ZipCode).HasColumnName("BillZipCode")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.State).HasColumnName("BillState")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.Country).HasColumnName("BillCountry")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.Email).HasColumnName("BillEmail")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.Phone).HasColumnName("BillPhone")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.FirstName).HasColumnName("BillFirstName")
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.BillAddress.LastName).HasColumnName("BillLastName")
                .HasMaxLength(50);

            //ShipAddress
          
           modelBuilder.Entity<Order>().Property(i => i.ShipAddress.Addr).HasColumnName("ShipAddr")
               .HasMaxLength(128)
               .IsRequired();//B:注意这里
            modelBuilder.Entity<Order>().Property(i => i.ShipAddress.Street).HasColumnName("ShipStreet")
                //.IsRequired()
                .HasMaxLength(128);
            modelBuilder.Entity<Order>().Property(i => i.ShipAddress.City).HasColumnName("ShipCity")
                // .IsRequired()
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.ShipAddress.ZipCode).HasColumnName("ShipZipCode")
                // .IsRequired()
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.ShipAddress.State).HasColumnName("ShipState")
               // .IsRequired()
                .HasMaxLength(50);
            modelBuilder.Entity<Order>().Property(i => i.ShipAddress.Country).HasColumnName("Ship