I am pretty new to coding ADO.Net/entity framework stuff. I am trying to follow "Microsoft ADO.NET Entity Framework Step by Step", Microsoft Press Mueller. I'm using VS 2012 with EF6 installed.
Start a new project, and adding an new Empty ADO.NET Entity Data Model. It's pretty simple, a few scalars and one Enumeration called UserFavorites. Then add a data source > Object > Drilling down to my UserFavorites object and finishing. I change toe UserFavorites object on the data source tab to detail view ( and a couple other changes like combo box and label on the others). Then drag the UserFavorites object to the form. It creates a Binding source and binding navigator all as it should. After enabling the save button and entering the code below it runs great and gets the records from the database that can be scrolled between. The problem is when I click the plus to add a record, fill it in, and click save, I get validation errors. on the
UserFavoritesContext.SaveChanges();
line. The first error is
"Exception:Thrown: "Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'." (System.ArgumentOutOfRangeException)"
There is a date picker on the form, and it's filled out correctly. If I remove that item from my model I get errors on the next item in the model. For some reason it's not pulling the data filled in on the form and trying to use defaults (or null).
I can't find anyone else with this problem online, so I guess I am missing something silly. I followed the book exactly ( and can't find a help forum for the book). I hope this is clear enough for someone to offer some guidance.
Form and Data Source
Model
Error
Here is a
zip [
^]of the project. Maybe it will help!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UserFavoritesEF6
{
public partial class Form1 : Form
{
UserFavoritesModelContainer UserFavoritesContext;
public Form1()
{
InitializeComponent();
UserFavoritesContext = new UserFavoritesModelContainer();
var dbQuery =
UserFavoritesContext.UserFavorites.Where(id => id.UserId >= 0).ToArray();
}
private void userFavoritesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
UserFavoritesContext.SaveChanges();
}
private void Form1_Load(object sender, EventArgs e)
{
userFavoritesBindingSource.DataSource =
UserFavoritesContext.UserFavorites.Local;
favoriteColorComboBox.DataSource = Enum.GetValues(typeof(ColorNames));
favoriteColorComboBox.SelectedItem = ColorNames.Red;
}
}
}