DbContext.SaveChanges();不会给出错误,但不会将实体添加到数据库
我正在尝试使用以下代码将实体保存到我的数据库中:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将新实体附加到上下文。尝试:
You have to attach the new entity to the context. Try: