EntityFramwork常见问题

1.常用的语句有哪些

 添加migration      dotnet ef migrations add [MIgrationName]

删除刚添加的migration      dotnet ef migrations remove

更新数据库    dotnet ef database update、

2.在代码中用 attribute (code first) 限制表中字段的类型及主外键

主键      [Key]
自增长  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

外键     [ForeignKey("【主表名】")]

字符串类型   [Column(TypeName = "NVARCHAR(50)")]

3.更新数据库报错 String or binary data would be truncated. The statement has been terminated.

 多半是字符串长度改短了 数据库中存在长字符串数据
 根据migration的提交记录看最后一次 哪些字段改了类型进行比对

4.如何添加外键

 添加外键的时候需要在主表中添加对应的从表的ICollection或者属性字段

  一对多  public ICollection<OrderDtl> OrderDtlList { get; set; }  

  一对一  public ExpressInfo ExpressInfo { get; set; } 

原文地址:https://www.cnblogs.com/ITCoNan/p/7753007.html