Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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!!
C#
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
    {
        // Define the context used to access the database.
        UserFavoritesModelContainer UserFavoritesContext;

        public Form1()
        {
            InitializeComponent();

            // Initialize the database context.
            UserFavoritesContext = new UserFavoritesModelContainer();

            // Query the database for the records you want.
            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)
        {

            // Assign a local copy of the queried records to the
            // binding source.
            userFavoritesBindingSource.DataSource =
               UserFavoritesContext.UserFavorites.Local;

            // Fill the Favorite Colors list with acceptable colors and
            // choose a default.
            favoriteColorComboBox.DataSource = Enum.GetValues(typeof(ColorNames));
            favoriteColorComboBox.SelectedItem = ColorNames.Red;
        }

 
    }
}
Posted
Updated 6-Apr-15 16:16pm
v2
Comments
Member 11428137 30-Mar-15 4:24am    
I have seen before with the date time picker, that if you don't pick a value, it will show the current date, but will actually save null to the datasource. Do you still have the problem if you change the date before saving?
prizzy 30-Mar-15 15:53pm    
Yeah. I even tried the project with no date/time picker. it then says "null" is not acceptable for the other fields. Even when I have entered data in them. So I went and allowed null in the database, but VS still threw the error. I seem to be the only one having this problem. it sucks.
prizzy 7-Apr-15 21:03pm    
I added the project link (dropbox). https://www.dropbox.com/s/vyul4dh29351eru/UserFavoritesEF6.zip?dl=0

try to put hard code / default date value...

if you can share code then might get solution.
as per this thread things are not very clear to me.
 
Share this answer
 
v2
Comments
prizzy 7-Apr-15 21:04pm    
Here is the project. https://www.dropbox.com/s/vyul4dh29351eru/UserFavoritesEF6.zip?dl=0

I will try your solution and respond. It happens with all the fields on the details form. Its not reading any of the form fields with .savechanges(). Using defaults for them all.
prizzy 7-Apr-15 22:47pm    
So that stops the error but inserts the default values in the DB. For some reason, .savechanges() is not reading the form data and saving it. Entity is supposed to update automatically without having to manually add a record with all it's individual fields.
Hello,

Now this is working at my side, i am able to add

Do change into UserFavorites Class

public
XML
System.DateTime Birthday { get; set; }

To

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public System.DateTime Birthday { get; set; }

I was getting error {"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}

--Steps
-- Changes code
-- Clean solution
-- Build
-- Must pres tab on each filed before save record


if you want whole code contact padam.production@gmail.com or share your id
 
Share this answer
 
v2
Comments
prizzy 8-Apr-15 22:16pm    
It worked, and I had to tab through all the fields. why is that? how do I avoid having to do that? Some forms may not need all the fields tabbed through. I had default values set.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900