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

 首先是

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

在来order

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

然后我们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("ShipCountry")
// .IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Order>().Property(i => i.ShipAddress.Email).HasColumnName("ShipEmail")
// .IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Order>().Property(i => i.ShipAddress.Phone).HasColumnName("ShipPhone")
// .IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Order>().Property(i => i.ShipAddress.FirstName).HasColumnName("ShipFirstName")
// .IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<Order>().Property(i => i.ShipAddress.LastName).HasColumnName("ShipLastName")
//.IsRequired()
.HasMaxLength(50);

注意A和B的地方,我们A标识可以为空,B不允许为空。

生成数据库后,问题出现了。

 

不知道你注意到了吗,两个地方都为不允许为空,怎么回事,求解...




posted @ 2012-03-16 17:23  Xia.CJ  阅读(1421)  评论(0编辑  收藏  举报