插入数据库时​​的数据实体模型配置modelvalidationexception [英] Data entity modelconfiguration modelvalidationexception when insert database

查看:77
本文介绍了插入数据库时​​的数据实体模型配置modelvalidationexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者实体框架

数据实体ModelConfiguration ModelValidationException当插入数据odatabase

我的代码有什么问题?

提前感谢!



这是我的对象类

i'm beginer to Entity Framework
Data Entity ModelConfiguration ModelValidationException when insert data odatabase
what is the wrong in my code?
Thank in advance!

This is my Object Class

[Table ("customerDetails")]
    class customerDetails
    {
        [Key]
        internal int cusId { get; set; }
        internal string cusName { get; set; }
        internal string cusNic { get; set; }
        internal string cusAdd { get; set; }
        internal DateTime cusDob { get; set; }
    }.





这是DbContext



This is DbContext

class cusSave : DbContext
    {
        public cusSave() : base(@"Server= servername"
                            + "; Database= dbname"
                            + "; UID= user"
                            + "; Pwd= pass") { }
        public DbSet<customerDetails> saveCus { get; set; }
    }





这是btn活动



This is the btn Event

private void btnSave_Click(object sender, EventArgs e)
        {
            var cus =new Logic.customerDetails
            {
                cusId = Convert.ToInt32(txtCusId.Text),
                cusName = txtcusName.Text,
                cusAdd = txtcusAdd.Text,
                cusNic = txtcusNic.Text,
                cusDob = dtpDob.Value
            };
                var saveCus = new Logic.cusSave();
                saveCus.saveCus.Add(cus);
                saveCus.SaveChanges();
        }







这里的解决方案

更改对象类这个

谢谢每一个人

特别 Graeme_Grant






Solution here
Change object class like this
Thanks Every One
Specially Graeme_Grant

class customerDetails
    {
        [Key]
        public int cusId { get; set; }
        public string cusName { get; set; }
        public string cusNic { get; set; }
        public string cusAdd { get; set; }
        public DateTime cusDob { get; set; }
    }





我尝试过:



在保存时检查所有对象值,当分配给对象但没有错误时



What I have tried:

check all object values before save when assign to object but nothings wrong

推荐答案

好的。类和/或属性的范围似乎是您的问题。将类和属性都更改为public,然后重试。
Okay. The scope of the class &/or properties appears to be your problem. Change both to the class and the properties public and try again.


这篇关于插入数据库时​​的数据实体模型配置modelvalidationexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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