EntityType [TableName] has no key defined. Define the key for this EntityType

Code First

1. 基于Model中 字段 ID / [className]+ID / 某字段有[Key] , 来自动生成主键的, 如果没有将会报如标题的错.

  public class Order{

    public int ID{get;set;}

  }

  public class Order{

    public int OrderID{get;set;}

  }

  public class Order{

    [Key]

    public int CustomID{get;set;}

  }

2. 当需要复合主键时,则要在各个主键上加 [Key, Column(Order=0)], [Key, Column(Order=1)] ... , Order 按列顺序递增设置.

  public class Order{

      [Key,Column(Order=0)]

      public string OrderKey{get;set;}

      [Key,Column(Order=1)]

      public string OrderName{get;set;}

  }

posted @ 2013-01-30 10:54  Yu  阅读(2395)  评论(0编辑  收藏  举报