EntityType X没有定义键。定义此EntityType的键

时间:2016-06-20 22:18:55

标签: c# entity-framework

所以我试图通过调用我已经传递了我想要保存的对象的静态类方法来保存数据库中的实体:

static public Room saveTheatherRoom(Room theatherRoom)
{
        using (var db = new ThetherDBContext())
        {
            db.Rooms.Add(theatherRoom);                                
            db.SaveChanges();
            return theatherRoom;
        }
}

db.Rooms.Add(theatherRoom);行上我收到以下异常:

    System.Data.Entity.ModelConfiguration.ModelValidationException was unhandled
  HResult=-2146233088
  Message=One or more validation errors were detected during model generation:

Theather.Image: : EntityType 'Image' has no key defined. Define the key for this EntityType.
Theather.ImageList: : EntityType 'ImageList' has no key defined. Define the key for this EntityType.
Theather.ToolStripItem: : EntityType 'ToolStripItem' has no key defined. Define the key for this EntityType.
Theather.Control: : EntityType 'Control' has no key defined. Define the key for this EntityType.
Theather.PrintDocument: : EntityType 'PrintDocument' has no key defined. Define the key for this EntityType.
Theather.PageSettings: : EntityType 'PageSettings' has no key defined. Define the key for this EntityType.
Theather.NumericUpDownAcceleration: : EntityType 'NumericUpDownAcceleration' has no key defined. Define the key for this EntityType.
Theather.DataGridViewCellStyle: : EntityType 'DataGridViewCellStyle' has no key defined. Define the key for this EntityType.
Theather.DataGridViewCell: : EntityType 'DataGridViewCell' has no key defined. Define the key for this EntityType.
Theather.DataGridViewRow: : EntityType 'DataGridViewRow' has no key defined. Define the key for this EntityType.
Theather.ListViewItem: : EntityType 'ListViewItem' has no key defined. Define the key for this EntityType.
Theather.CultureInfo: : EntityType 'CultureInfo' has no key defined. Define the key for this EntityType.
Theather.DateTimeFormatInfo: : EntityType 'DateTimeFormatInfo' has no key defined. Define the key for this EntityType.
Theather.TreeNode: : EntityType 'TreeNode' has no key defined. Define the key for this EntityType.
GridItems: : The referenced EntitySet 'GridItems' for End 'GridEntry_ParentGridEntry_Source' could not be found in the containing EntityContainer.
GridItems: : The referenced EntitySet 'GridItems' for End 'GridEntry_ParentGridEntry_Target' could not be found in the containing EntityContainer.
LayoutSettings: : The referenced EntitySet 'LayoutSettings' for End 'TableLayoutPanel_LayoutSettings_Target' could not be found in the containing EntityContainer.
Images: EntityType: EntitySet 'Images' is based on type 'Image' that has no keys defined.
ImageLists: EntityType: EntitySet 'ImageLists' is based on type 'ImageList' that has no keys defined.
ToolStripItems: EntityType: EntitySet 'ToolStripItems' is based on type 'ToolStripItem' that has no keys defined.
Controls: EntityType: EntitySet 'Controls' is based on type 'Control' that has no keys defined.
PrintDocuments: EntityType: EntitySet 'PrintDocuments' is based on type 'PrintDocument' that has no keys defined.
PageSettings: EntityType: EntitySet 'PageSettings' is based on type 'PageSettings' that has no keys defined.
NumericUpDownAccelerations: EntityType: EntitySet 'NumericUpDownAccelerations' is based on type 'NumericUpDownAcceleration' that has no keys defined.
DataGridViewCellStyles: EntityType: EntitySet 'DataGridViewCellStyles' is based on type 'DataGridViewCellStyle' that has no keys defined.
DataGridViewCells: EntityType: EntitySet 'DataGridViewCells' is based on type 'DataGridViewCell' that has no keys defined.
DataGridViewRows: EntityType: EntitySet 'DataGridViewRows' is based on type 'DataGridViewRow' that has no keys defined.
ListViewItems: EntityType: EntitySet 'ListViewItems' is based on type 'ListViewItem' that has no keys defined.
CultureInfoes: EntityType: EntitySet 'CultureInfoes' is based on type 'CultureInfo' that has no keys defined.
DateTimeFormatInfoes: EntityType: EntitySet 'DateTimeFormatInfoes' is based on type 'DateTimeFormatInfo' that has no keys defined.
TreeNodes: EntityType: EntitySet 'TreeNodes' is based on type 'TreeNode' that has no keys defined.
GridItems: EntityType: EntitySet 'GridItems' is based on type 'GridItem' that has no keys defined.
LayoutSettings: EntityType: EntitySet 'LayoutSettings' is based on type 'LayoutSettings' that has no keys defined.

这显然没有任何意义,因为我的数据库上下文或实体类中没有上述EntityType。

2 个答案:

答案 0 :(得分:1)

根据我的经验,ModelValidationException错误通常可能导致数据库数据上下文中的表类缺少KeyAttribute,或者Database.SetInitializerOnModelCreating重写方法中错误地初始化

检查您是否满足以下条件:

如果您有edmx(实体框架)或dbml(LINQ to SQL),请确保所有表    类具有KeyAttribute来标识主键列属性。

using System.ComponentModel.DataAnnotations;

namespace Theater
{
    public class Room
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // only if your primary key is auto-generated/identity column
        [Key]
        public int RoomId { get; set; }
    }
}

其次,确保你的方法有" public static"按顺序排列。

public static Room saveTheatherRoom(Room theatherRoom)
{
        using (var db = new ThetherDBContext())
        {
            db.Rooms.Add(theatherRoom);                                
            db.SaveChanges();
            return theatherRoom;
        }
}

然后,检查DbContext构造函数是否具有" Rooms"作为DbSet属性。

public ThetherDBContext() : base("ConnectionName")
{
    public DbSet<Room> Rooms { get; set; }
}

CMIIW。

答案 1 :(得分:1)

好的,错误是由以下原因造成的: 我从entityframework上下文文件中的类派生了一个帮助器类。我没有尝试通过上下文在数据库中保存该帮助程序类的任何实例,但无论如何EF都以某种方式完成了它。

派生类:

class SeatHelper : Seat
{
    public int typeId { get; set; }
    public bool seatPositionSet { get; set; }
    public PictureBox image { get; set; }
    public Point position { get; set; }
}

家长班:

[Table("Seat")]
public partial class Seat
{
    public Seat()
    {
        ReservedSeats = new HashSet<ReservedSeat>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int seatId { get; set; }

... etc.

上下文文件:

public partial class ThetherDBContext : DbContext
{
    public ThetherDBContext()
        : base("name=ThetherDBContext")
    {
    }

    public virtual DbSet<Room> Rooms { get; set; }
    public virtual DbSet<TypeOfSeat> TypesOfSeats { get; set; }
    public virtual DbSet<Seat> Seats { get; set; } ... etc.

解决方法是从SeatHelper类中删除继承。