我有两个简单的类,它们相互引用为下面定义的一对多关系:
public class Project
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Document> Documents { get; set; }
}
public class Document
{
public virtual int Id { get; set; }
public string FileName { get; set; }
}
我的映射定义为:
public class ProjectMapping : ClassMap<Project>
{
public ProjectMapping()
{
Table("Projects");
Id(x => x.Id).Column("Project_Id").GeneratedBy.TriggerIdentity();
HasMany(x => x.Documents)
.Table("Documents")
.KeyColumn("Document_Project_Id")
.Cascade.AllDeleteOrphan()
.Not.KeyNullable();
Map(x => x.Name).Column("Project_Name");
}
}
public class DocumentMapping : ClassMap<Document>
{
public DocumentMapping()
{
Table("Documents");
Id(x => x.Id).Column("Document_Id").GeneratedBy.TriggerIdentity();
Map(x => x.FileName).Column("Document_File_Name");
}
}
一切似乎都工作正常,添加/更新文档并调用 session.Save(project) 反映了我的数据库中的正确更改,但是如果我要从与项目关联的文档列表中删除文档并调用 session.Save(项目)删除的文档永远不会从数据库中删除。
任何想法为什么除了删除之外其他所有东西都可以工作?
编辑: 我的 MVC 4 项目是使用 Fluent NHibernate 设置的,如下所示:
public class SessionFactoryHelper
{
public static ISessionFactory CreateSessionFactory()
{
var c = Fluently.Configure();
try
{
//Replace connectionstring and default schema
c.Database(OdbcConfiguration.MyDialect.
ConnectionString(x =>
x.FromConnectionStringWithKey("DBConnect"))
.Driver<NHibernate.Driver.OdbcDriver>()
.Dialect<NHibernate.Dialect.Oracle10gDialect>())
.ExposeConfiguration(cfg => cfg.SetProperty("current_session_context_class", "web"));
c.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Project>());
c.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Document>());
}
catch (Exception ex)
{
Log.WriteLine(ex.ToString());
}
return c.BuildSessionFactory();
}
}
public class MvcApplication : System.Web.HttpApplication
{
public static ISessionFactory SessionFactory { get; private set; }
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
SessionFactory = SessionFactoryHelper.CreateSessionFactory();
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
}
protected void Application_EndRequest(object sender, EventArgs e)
{
var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();
}
}
我的存储库定义如下:
public class Repository<T> : IRepository<T>
{
public virtual ISession Session
{
get { return MvcApplication.SessionFactory.GetCurrentSession(); }
}
public T FindById(int iId)
{
return Session.Get<T>(iId);
}
public void Save(T obj)
{
using (var transaction = Session.BeginTransaction())
{
try
{
Session.Save(obj);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
Log.WriteLine(ex.ToString());
}
}
}
public T SaveOrUpdate(T obj)
{
using (var transaction = Session.BeginTransaction())
{
try
{
Session.SaveOrUpdate(obj);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
Log.WriteLine(ex.ToString());
}
}
return obj;
}
public T Update(T obj)
{
using (var transaction = Session.BeginTransaction())
{
try
{
Session.Update(obj);
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
Log.WriteLine(ex.ToString());
}
}
return obj;
}
}
我在我的 ProjectsController 中定义了 2 个操作,如下所示:
private IRepository<Project> repository;
public ProjectsController()
{
repository = new Repository<Project>();
}
public ActionResult Edit(int iId)
{
Project project = repository.FindById(iId);
if (project == null)
return HttpNotFound();
return View(project);
}
[HttpPost]
public ActionResult Edit(Project project)
{
project = repository.Update(project);
return View(project);
}
如果我要在我的第一个操作中删除一个文档(没有 HttpPost):
project.Documents.RemoveAt(0);
repository.Update(project);
从数据库中删除正确的行。但是,如果我要在具有 HttpPost 属性的操作中执行相同的操作,则永远不会删除该行。
另外我应该注意,如果我在具有 HttpPost 属性的操作中向 project.Documents 添加一个文档,repository.Update(project) 会成功地将具有正确外键引用的行添加到项目中。这仅在我删除文档时失败。