Reputation: 8824
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);
}
Upvotes: 3
Views: 5950
Reputation: 48975
You have to attach the new entity to the context. Try:
context.ElectronicSignatureTypes.Add(electronicSignatureType);
context.SaveChanges();
Upvotes: 5