【问题标题】:Entity error when there is no input DateTimePicker没有输入 DateTimePicker 时出现实体错误
【发布时间】:2020-01-02 23:29:11
【问题描述】:
internal class Person
    {
        [Key]
        public int PersonId { get; set; }

        [Required]
        [StringLength(20)]
        public string SurName { get; set; }

        [Required]
        [StringLength(20)]
        public string LastName { get; set; }

        [Required]
        public DateTime BirthDate { get; set; }

        public DateTime DeathDate { get; set; }

        [Required]
        public Gender gender { get; set; }

        [Required]
        [StringLength(20)]
        public string Father { get; set; }

        [Required]
        [StringLength(20)]
        public string Mother { get; set; }



 public Person(string surName, string lastName, DateTime birthDate, Gender gender, string father, string mother)
    {
        SurName = surName;
        LastName = lastName;
        BirthDate = birthDate;
        this.gender = gender;
        Father = father;
        Mother = mother;
    }

DeathDate 没有 [required],但只有在没有 DeathDate 输入时才会崩溃。

当我想将它们添加到数据库时,调试器中显示的错误

抛出异常:“System.Data.SqlClient.SqlException”在 EntityFramework.dll

private void AddPersonButton_Click(object sender, RoutedEventArgs e)
    {
        if (DeathDateDatePicker.SelectedDate == null)
        {
             Person personsntDeath = new Person(
                SurNameTextBlock.Text.Trim(),
                LastNameTextBlock.Text.Trim(),
                BirthDateDatePicker.SelectedDate.Value,
                (Gender)SexComboBox.SelectedItem,
                FatherTextBox.Text.Trim(),
                MotherTextBox.Text.Trim());

            personsntDeath.InsertIntoDB();
        }
        else
        {

            Person persons = new Person(
                SurNameTextBlock.Text.Trim(),
                LastNameTextBlock.Text.Trim(),
                BirthDateDatePicker.SelectedDate.Value,
                DeathDateDatePicker.SelectedDate.Value,
                (Gender)SexComboBox.SelectedItem,
                FatherTextBox.Text.Trim(),
                MotherTextBox.Text.Trim());

            persons.InsertIntoDB();
        }

        mainWindow.Content = new PageOverviewPersons(mainWindow);
    }

如果 DeathDatePicker 中没有输入,我希望它在我的数据库中为空。它返回值“{01/01/0001 00:00:00}”

public bool InsertPerson(Person persons)
    {
        using (DataBaseContext ctx = new DataBaseContext())
        {
            try
            {
                ctx.Persons.Add(persons);
                ctx.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

我正在尝试创建一个家谱程序。

【问题讨论】:

  • 你为什么不让它可以为空DateTime?
  • 您是否尝试将DeathDate 标记为可选?
  • 这两件事都试过了

标签: c# entity-framework datetime sqlexception


【解决方案1】:

DateTime 是一个值类型,除非您使用 ? 运算符将其定义为可空,否则它不能为空

public DateTime? DeathDate { get; set; }

【讨论】:

  • 我试过了,但仍然报错:Exception throw: 'System.Data.SqlClient.SqlException' in EntityFramework.dll
  • 您是否在将属性更改为可为空后更新了数据库?如果您使用代码优先,只需添加迁移
  • 它只是给了我一个新错误:Exception throw: 'System.Data.Entity.Infrastructure.DbUpdateException' in EntityFramework.dll
  • 您可以检查内部异常以获取更多详细信息吗?这样你就可以知道是什么属性导致了错误
  • @KylianNijdam 如果您在尝试运行迁移时看到这种情况,这通常意味着您的连接字符串中定义的用户没有修改表的正确权限。
【解决方案2】:

尝试改变

public DateTime DeathDate { get; set; }

public DateTime? DeathDate { get; set; } //Nullable

并使用DateTime?.HasValue 属性检查它是否获得了价值。

if (!DeathDateDatePicker.SelectedDate.HasValue)
{
    //  Your code here
}

【讨论】:

    猜你喜欢
    • 2015-04-27
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2022-01-05
    • 2014-08-12
    • 2011-04-12
    • 2015-06-05
    • 2022-08-11
    相关资源
    最近更新 更多