遇到简单的EF6(C#)Winform从详细信息表格中保存数据的问题 [英] Having problems with simple EF6 (C#) Winform saving data from details form

查看:531
本文介绍了遇到简单的EF6(C#)Winform从详细信息表格中保存数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很擅长编码ADO.Net/entity框架的东西。我试图按照Microsoft ADO.NET实体框架一步一步,Microsoft Press Mueller。我正在使用安装了EF6的VS 2012。



启动一个新项目,并添加一个新的空ADO.NET实体数据模型。它很简单,一些标量和一个名为UserFavorites的枚举。然后添加数据源>对象>深入到我的UserFavorites对象并完成。我将数据源选项卡上的脚趾UserFavorites对象更改为详细视图(以及其他一些更改,如组合框和其他标签)。然后将UserFavorites对象拖到窗体中。它创建了一个Binding源和绑定导航器。启用保存按钮并输入下面的代码后,它运行得很好,并从数据库中获取可以在其间滚动的记录。问题是当我单击加号添加记录,填写它,然后单击保存,我得到验证错误。在

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();

行。第一个错误是

Exception:Thrown:'1/1/0001 12:00:00 AM'的值对'Value'无效。 'Value'应该在'MinDate'和'MaxDate'之间。(System.ArgumentOutOfRangeException)



表格上有一个日期选择器,它已填写完整正确。如果我从模型中删除该项目,我会在模型中的下一个项目上出错。由于某种原因,它不会拉动表单上填写的数据并尝试使用默认值(或null)。



我在网上找不到其他有这个问题的人,所以我想我错过了一些傻事。我完全按照这本书(并没有找到该书的帮助论坛)。我希望这足以让某人提供一些指导。



表格和数据来源



型号



错误



这是 zip [ ^ ]。也许它会有所帮助!!

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
    {
        // 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;
        }

 
    }
}

推荐答案

尝试放硬编码/默认日期值...



如果你可以共享代码然后可能得到解决方案。

根据这个帖子我不太清楚。
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.


你好,



现在这是在我这边工作,我可以添加



更改为UserFavorites类



public
Hello,

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

Do change into UserFavorites Class

public
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


这篇关于遇到简单的EF6(C#)Winform从详细信息表格中保存数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆