Reputation: 367
So I am trying to save the entity in database by calling the static class method to which I have passed the object I'd like to save:
static public Room saveTheatherRoom(Room theatherRoom)
{
using (var db = new ThetherDBContext())
{
db.Rooms.Add(theatherRoom);
db.SaveChanges();
return theatherRoom;
}
}
on line db.Rooms.Add(theatherRoom);
I get the following exception:
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.
which clearly doesn't make any sense since I have none of the above EntityTypes in my database context or entity classes.
Upvotes: 1
Views: 6388
Reputation: 367
Ok, the error was caused by the following: I derived one helper class from the class present in entityframework context file. I didn't try to save any instances of that helper class in database via context, but EF somehow did it anyway.
Derived class:
class SeatHelper : Seat
{
public int typeId { get; set; }
public bool seatPositionSet { get; set; }
public PictureBox image { get; set; }
public Point position { get; set; }
}
Parent class:
[Table("Seat")]
public partial class Seat
{
public Seat()
{
ReservedSeats = new HashSet<ReservedSeat>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int seatId { get; set; }
... etc.
Context file:
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.
Solution was to remove the inheritance from the SeatHelper class.
Upvotes: 1
Reputation: 24957
From my experience, ModelValidationException
error usually may either cause from missing of KeyAttribute
at table classes inside DB data context, or Database.SetInitializer
has wrongly initialized in OnModelCreating
overridden method.
Check whether you've satisfy these conditions:
If you have edmx (Entity Framework) or dbml (LINQ to SQL), make sure all table
classes have KeyAttribute
to identify primary key column property.
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; }
}
}
Second, make sure your methods have "public static" in proper order.
public static Room saveTheatherRoom(Room theatherRoom)
{
using (var db = new ThetherDBContext())
{
db.Rooms.Add(theatherRoom);
db.SaveChanges();
return theatherRoom;
}
}
Then, check if DbContext constructor has "Rooms" as DbSet property.
public ThetherDBContext() : base("ConnectionName")
{
public DbSet<Room> Rooms { get; set; }
}
CMIIW.
Upvotes: 1