Reputation: 1
I am trying to save the changes here and I am getting an exception error on db.SaveChanges(); I am trying to enter some content to the database and its saying that it needs to be validated.
Here is the error:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
using (GameConnection db = new GameConnection())
{
Game newGame = new Game();
int GameID = 0;
if (Request.QueryString.Count > 0)
{
GameID = Convert.ToInt32(Request.QueryString["gameID"]);
newGame = (from game in db.Games
where game.gameID == GameID
select game).FirstOrDefault();
}
newGame.teamA = teamATextBox.Text;
newGame.teamB = teamBTextBox.Text;
if (GameID == 0)
{
db.Games.Add(newGame);
}
db.SaveChanges();
Response.Redirect("~/Default.aspx");
}
}
Upvotes: 0
Views: 5414
Reputation: 30022
The error is stating that at least one of the validation rules failed before submitting the request to the database.
What you need to do is check your class definition for Game
(or database table) and check for NotNull
or other restrictions and check if your input matches all these constraints by debugging the Game
object before db.SaveChanges();
Even better, add a catch to print to the Console
the Properties
that failed:
using (GameConnection db = new GameConnection())
{
Game newGame = new Game();
int GameID = 0;
if (Request.QueryString.Count > 0)
{
GameID = Convert.ToInt32(Request.QueryString["gameID"]);
newGame = (from game in db.Games
where game.gameID == GameID
select game).FirstOrDefault();
}
newGame.teamA = teamATextBox.Text;
newGame.teamB = teamBTextBox.Text;
if (GameID == 0)
{
db.Games.Add(newGame);
}
try
{
db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine(ve.PropertyName + " " + ve.ErrorMessage);
}
}
Response.Redirect("~/Default.aspx");
}
Upvotes: 2