如何在C#中修复'entityvalidationerrors' [英] How to fix 'entityvalidationerrors' in C#

查看:100
本文介绍了如何在C#中修复'entityvalidationerrors'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I tried to make the application and connect it to the SQL Server. but I have a problem with the following line of code.







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;
using System.IO;

namespace CBIR
{
    public partial class training : Form
    {
        string fileName;
        public training()
        {
            InitializeComponent();
        }

        private void Pilihgbr_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog() { Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|All Files(*.*)|*.*", ValidateNames = true, Multiselect = false })
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    fileName = dialog.FileName;
                    pictureBox1.Image = Image.FromFile(fileName);
                }
            }
        }

        byte[] ConvertImage (Image img)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
        }

        private async void Mulaitraining_Click(object sender, EventArgs e)
        {
            using (cbirEntities db = new cbirEntities())
            {
                tabel_cbir pic = new tabel_cbir() { Nama_Gambar = fileName, Gambar = ConvertImage (pictureBox1.Image)};
                db.tabel_cbir.Add(pic);
                await db.SaveChangesAsync();
                MessageBox.Show("Tersimpan", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void balik_Click(object sender, EventArgs e)
        {
            menuutama m1 = new menuutama();
            m1.Show();
            this.Close();
        }
    }
}







and an error occurred while running the program on the line.







await db.SaveChangesAsync();





错误信息是'EntityValidationError',如何解决?





the error message is 'EntityValidationError', How to fix it?

Any help will be appreciated. Thanks.





我尝试过:



我尝试使用try-catch来修复它,但它仍然无效。



What I have tried:

I tried using try-catch to fix it, but it's still not work.

推荐答案

try ... catch 阻止不要修复问题:它们让你正确处理它们,或者至少优雅地处理它们。



你需要做的就是看看您传递给数据库的数据,将其与表中的实际字段进行比较,并确定哪些数据类型错误:可能是名称而不是数字,或者日期无效。使用调试器并找出传递的内容然后与数据库设计进行比较。



获得该信息后,您可以查看数据的来源,以及添加验证以确保坏数据不会那么远 - 它可能来自用户,并且应该始终被验证并尽快转换为本机类型,这样您就不会丢失用户上下文,并且可以给他们及时的机会来纠正它。
try ... catch blocks don;t "fix" problems: they let you handle them properly, or at least gracefully.

What you need to do is look at the data you are passing to your DB, compare that to the actual fields in your tables, and work out what data is the wrong type: a name instead of a number perhaps, or an invalid date. Use the debugger and find out what is being passed then compare against the DB design.

When you have that information, you can look at where that data came from, and add validation to ensure that the "bad data" doesn't get that far - it's probably from user into, and that should always be validated and converted to "native types" as quickly as possible so you don't lose user context, and can give them a timely chance to correct it.


你的实体中的一个或多个属性无效。在失败的行上放置一个断点,运行代码,当调试器停止时,检查您的实体以确保属性有效。
One or more of the properties in your entity are not valid. put a breakpoint on the line that is failing, run the code, and when the debugger stops, inspect your entity to make sure the properties are valid.


这篇关于如何在C#中修复'entityvalidationerrors'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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