简体   繁体   English

将记录添加到数据库C#时发生DbUpdateException

[英]DbUpdateException occuring when adding a record to a database C#

This is the first time I have used EntityModels and I am having trouble adding a new record to the database. 这是我第一次使用EntityModels,但是在向数据库添加新记录时遇到麻烦。 An 'DbUpdateException' occurs in the SaveChanges() method, also I can not find the "Inner Exception". SaveChanges()方法中发生“ DbUpdateException”,我也找不到“内部异常”。

DbUpdateException

    //this is the code in the Bookings class
    public void Save()
    {
        InsightEntities ie = new InsightEntities();

        ie.Bookings.Add(this);

        try
        {
            ie.SaveChanges(); //Error Occuring Here
        }
        catch (Exception err)
        {
            throw;
        }
    }

In case you need it here is the record I am trying to add: (BookingId & DateEntered have default values in the database) 如果您需要它,这是我要添加的记录:(BookingId和DateEntered在数据库中具有默认值)

类:预订

Thanks in advance 提前致谢

Turns out it wasn't inserting the new record as it couldn't convert DateTime to smalldatetime, 事实证明,由于无法将DateTime转换为smalldatetime,因此没有插入新记录,

Fixed this by changing the database column types to DateTime 通过将数据库列类型更改为DateTime来解决此问题

All works now! 现在一切正常! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM