DbContext.SaveChanges();不会给出错误,但不会将实体添加到数据库

发布于 2024-12-28 12:41:58 字数 1441 浏览 5 评论 0原文

我正在尝试使用以下代码将实体保存到我的数据库中:

    public void Add(object entity)
        {
            DbContext.Entry(entity).State = System.Data.EntityState.Added;
            DbContext.SaveChanges();
        } 

我测试过,如果出现问题(例如实体的字段为空,数据库中不能为空),则会给出错误。我填写了所有必要的字段并调用了这段代码。它没有给出错误。但是,该实体也没有添加到数据库中。我怎样才能找到 DbContext.SaveChanges(); 的位置出现错误但没有错误消息?

下面是调用 Add() 函数的代码。

  public void StoreElectronicSignatureType(ElectronicSignatureTypeModel model)
    {
        var RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
        var parameters = RSA.ExportParameters(false);
        model.modulus = Convert.ToBase64String(parameters.Modulus);
        model.exponent = Convert.ToBase64String(parameters.Exponent);
        model.privatekey = RSA.ToXmlString(true);

        ElectronicSignatureType electronicSignatureType = new ElectronicSignatureType();
        Entity entity =  GetEntity(model.entity);
        electronicSignatureType.Entity = entity;
        electronicSignatureType.HashAlgorithm = model.hashAlgorithm;
        electronicSignatureType.Exponent = model.exponent;
        electronicSignatureType.Modulus = model.modulus;
        electronicSignatureType.Version = model.version;
        //electronicSignatureType.EntityId = entity.EntityId;
        electronicSignatureType.PrivateKey = StrToByteArray(model.privatekey);
        Add(electronicSignatureType);
    }

I'm trying to save an entity to my database using this code:

    public void Add(object entity)
        {
            DbContext.Entry(entity).State = System.Data.EntityState.Added;
            DbContext.SaveChanges();
        } 

I tested and if something is wrong (e.g. a field of the entity is null that can't be null in the datbase) it gives an error. I filled in all the necesarry fields and called this code. It did not give an error. However, the entity also didn't get added to the database. How can I find out where DbContext.SaveChanges(); goes wrong without an error message?

Below is the code that calls the Add() function.

  public void StoreElectronicSignatureType(ElectronicSignatureTypeModel model)
    {
        var RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
        var parameters = RSA.ExportParameters(false);
        model.modulus = Convert.ToBase64String(parameters.Modulus);
        model.exponent = Convert.ToBase64String(parameters.Exponent);
        model.privatekey = RSA.ToXmlString(true);

        ElectronicSignatureType electronicSignatureType = new ElectronicSignatureType();
        Entity entity =  GetEntity(model.entity);
        electronicSignatureType.Entity = entity;
        electronicSignatureType.HashAlgorithm = model.hashAlgorithm;
        electronicSignatureType.Exponent = model.exponent;
        electronicSignatureType.Modulus = model.modulus;
        electronicSignatureType.Version = model.version;
        //electronicSignatureType.EntityId = entity.EntityId;
        electronicSignatureType.PrivateKey = StrToByteArray(model.privatekey);
        Add(electronicSignatureType);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2025-01-04 12:41:58

您必须将新实体附加到上下文。尝试:

context.ElectronicSignatureTypes.Add(electronicSignatureType);
context.SaveChanges();

You have to attach the new entity to the context. Try:

context.ElectronicSignatureTypes.Add(electronicSignatureType);
context.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文