本文介绍了Sql 2008与NHibernate Filestream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在sql server 2008中使用Filestream来存储用户上传的图像。我的问题是,NHibernate不会错误,但它也不会保存数据进入数据库。

下面的Image类是一个自定义的类(不要和System.Drawing.Image混淆)

  public class ImageMap:ClassMap< Image> 
{
public ImageMap()
{
WithTable(Images);

Id(x => x.ImageId).GeneratedBy.GuidComb()。WithUnsavedValue(Guid.Empty);

Map(x => x.ImageData);
Map(x => x.Extension);

参考文献(x => x.Owner,UserId);




$ b我的保存方法如下所示:

  public void Save(Image image)
{
ISession session = GetSession();

session.SaveOrUpdate(image);也许我保存错了,也许我的映射是关闭的。 ImageData是数据库中的varbinary(max)字段。

解决方案

答案很简单。我删除了我的覆盖保存方法,它修复了自己。我们的基类已经定义了保存方法,我的覆盖是错误的。

  public void Save(T entity)

使用(ITransaction tx = session.BeginTransaction())
{
session.SaveOrUpdate(entity);使用(ISession session = GetSession())

tx.Commit();
}
}


I am attempting to use Filestream in sql server 2008 to store user uploaded images.

My problem is that NHibernate will not error, but it also will not save the data into the database. No record is created.

The Image class below is a custom class (not to be confused with System.Drawing.Image)

public class ImageMap : ClassMap<Image>
{
    public ImageMap()
    {
        WithTable("Images");

        Id(x => x.ImageId).GeneratedBy.GuidComb().WithUnsavedValue(Guid.Empty);

        Map(x => x.ImageData);
        Map(x => x.Extension);

        References(x => x.Owner, "UserId");
    }
}

My method to save looks like this:

public void Save(Image image)
    {
        ISession session = GetSession();

        session.SaveOrUpdate(image);
    }

Maybe I'm saving wrong, maybe my mapping is off. ImageData is a varbinary(max) field in the database.

解决方案

The answer to this was simple. I deleted my override to the save method and it fixed itself. Our baseclass already had the save method defined and my override was just plain wrong.

public void Save(T entity)
    {
        using (ISession session = GetSession())
        using (ITransaction tx = session.BeginTransaction())
        {
            session.SaveOrUpdate(entity);
            tx.Commit();
        }
    }

这篇关于Sql 2008与NHibernate Filestream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:22