惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
腾讯CDC
博客园 - Franky
S
SegmentFault 最新的问题
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
I
InfoQ
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed
博客园 - 叶小钗
博客园_首页
有赞技术团队
有赞技术团队
雷峰网
雷峰网
量子位
小众软件
小众软件
月光博客
月光博客
U
Unit 42
D
DataBreaches.Net

博客园 - Xia.CJ

C# 委托 node.js中连接mongodb(Please ensure that you set the default write...)解决办法 node.js api解读之file system模块 Node.js小技巧 在windows上安装新版node.js-node.js学习笔记 Asp.Net MVC部暑托管服务器iis7提示403错误解决方法 mvc3中使用unobtrusive时,ajax更新加载页面后验证失效解决方法 ashx中使用HttpContext.Current.Session ,出现未将对象引用设置到实例上 搜索引擎(google/百度)高级指令 在mvc3中使用uploadify上传组件User.isAuthenticated等于false解决方法 MVC中用Html.RenderPartial还是Html.RenderAction或者Html.Partial-Xia.CJ 在_Layout使用Html.RenderAction的问题-MVC3(Razor)问题 无法删除此对象,因为未在 ObjectStateManager 中找到它-entity framework删除时 Javascript数组(array)操作 序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用 entity framework多对多关系查询 JQuery获取checkbox值,checkbox全选,操作checkbox JQuery操作Select JQuery动态创建DOM、表单
entity framework(EF)_code first复杂类型(Complex Types)问题
Xia.CJ · 2012-03-16 · via 博客园 - Xia.CJ

 首先是

 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不允许为空。

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

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