Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityType X没有定义键。定义此EntityType的键_C#_Entity Framework - Fatal编程技术网

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

C# EntityType X没有定义键。定义此EntityType的键,c#,entity-framework,C#,Entity Framework,因此,我试图通过调用静态类方法将实体保存在数据库中,我已将要保存的对象传递给该静态类方法: static public Room saveTheatherRoom(Room theatherRoom) { using (var db = new ThetherDBContext()) { db.Rooms.Add(theatherRoom); db.Sa

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

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
错误通常可能是由于数据库数据上下文中的表类中缺少
KeyAttribute
导致的,或
Database.SetInitializer
OnModelCreating
重写方法中错误初始化

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

如果您有edmx(实体框架)或dbml(LINQ到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 ThetherDBContext() : base("ConnectionName")
{
    public DbSet<Room> Rooms { get; set; }
}
第二,确保您的方法具有顺序正确的“publicstatic”

public static Room saveTheatherRoom(Room theatherRoom)
{
        using (var db = new ThetherDBContext())
        {
            db.Rooms.Add(theatherRoom);                                
            db.SaveChanges();
            return theatherRoom;
        }
}
然后,检查DbContext构造函数是否将“Rooms”作为DbSet属性

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 ThetherDBContext() : base("ConnectionName")
{
    public DbSet<Room> Rooms { get; set; }
}
public ThetherDBContext():base(“ConnectionName”)
{
公共数据库集房间{get;set;}
}

CMIIW.

正常,错误是由以下原因引起的: 我从entityframework上下文文件中的类派生了一个助手类。我并没有试图通过上下文在数据库中保存那个helper类的任何实例,但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.
[表格(“座位”)]
公共部分舱位
{
公众席()
{
ReservedSeats=newHashSet();
}
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int seatId{get;set;}
等
上下文文件:

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.
public分部类ThetherDBContext:DbContext
{
公共ThetherDBContext()
:base(“name=ThetherDBContext”)
{
}
公共虚拟数据库集房间{get;set;}
公共虚拟数据库集类型{get;set;}
公共虚拟数据库集座位{get;set;}…等。

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

您的方法签名不应该定义为public然后是static吗?无论如何,我会设置一些断点并从头到尾逐步检查您的代码,并确保传递的值是您所期望的值。同时确保dbset Rooms已实际定义作为dbset{get;set;}在DbContextThank中,感谢您的回答,但错误似乎是因为我继承了一个与windows窗体组件无关的类,它的基类也在数据库上下文中。我很高兴您能理解这一点。您应该为自己的问题写一个简短的答案,并给出解释,以便搜索此错误的其他人可以找到您的答案解决方案很有帮助。谢谢,我添加了更详细的解决方案。谢谢您的回答,但错误似乎是因为我继承了一个与windows窗体组件无关的类,其基类也在数据库上下文中。