Hi,
I'm having a problem when I violate a contraint. Suppose you have a DEPARTMENT table and also an EMPLOYEE table. EMPLOYEE has a foreign key referencing DEPARTAMENT (EMPLOYEE -> DEPARTMENT). You create only one department and also some employees which points to that department. Then you try to delete the department. What happens? And ADOException: could not delete etc. Nothing wrong here, I have to deal with this in my code. But even when I handle that exception and show some friendly message to the user, the same exception is raised again and again when I later try to add, alter or delete anything on the database! The same happens when I try to insert a record with an already existing primary key.
What am I missing here? :(
Here is some code that can help you figure out what's wrong. IMPORTANT NOTE: It's a WinForms app and the Session lives while the app is running (it never gets closed).
Code:
protected void Save(object obj) {
ITransaction transaction = PersistenceEngine.Session.BeginTransaction();
try {
Session.SaveOrUpdate(obj);
transaction.Commit();
} catch (Exception) {
transaction.Rollback();
throw;
}
}
protected void Delete(object obj) {
ITransaction transaction = PersistenceEngine.Session.BeginTransaction();
try {
Session.Delete(obj);
transaction.Commit();
} catch (Exception) {
transaction.Rollback();
throw;
}
}
Code:
internal class PersistenceEngine {
private static ISessionFactory sessionFactory;
private static ISession session;
private PersistenceEngine() { }
private static ISessionFactory SessionFactory {
get {
if (sessionFactory == null) {
Configuration configuration = new Configuration();
configuration.AddClass(typeof(Profissao));
configuration.AddClass(typeof(Estado));
configuration.AddClass(typeof(Municipio));
configuration.AddClass(typeof(Bairro));
configuration.AddClass(typeof(Paciente));
sessionFactory = configuration.BuildSessionFactory();
}
return sessionFactory;
}
}
public static ISession Session {
get {
if (session == null)
session = SessionFactory.OpenSession();
return session;
}
}
public static void Close() {
if (session != null)
session.Close();
}
}
May someone help me?
Thank you,
Celio