繁体   English   中英

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

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

因此,我试图通过调用将要保存的对象传递给的静态类方法来将实体保存在数据库中:

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。

根据我的经验, ModelValidationException错误通常可能是由于DB数据上下文内的表类缺少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 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。

好的,该错误是由以下原因引起的:我从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类中删除继承。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM