誰かがこれを理解するのを手伝ってくれるなら、私はそれを大いに感謝します. まず、次のようなクラスがあります。
public class Blob
{
public int BlobID { get; set; }
public string BlobName { get; set; }
public string FileName { get; set; }
public string FileMimeType { get; set; }
public int FileSize { get; set; }
public byte[] FileContent{ get; set; }
public DateTime DateCreated { get; set; }
public int CreatedByProfileID { get; set; }
}
かなり標準的で、まったく同じフィールド名を持つテーブルにマップされるオブジェクトです。SQL Server のテーブルは次のようになります。
私のコントローラーには、DB への読み取りと書き込みを行うためのアクションの追加と表示があります。以下のアクション コードを使用して、ファイルを正常に書き込むことができます。
[HttpPost]
public ActionResult Add(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
Database db = DatabaseFactory.CreateDatabase("dbconnstr");
byte[] fileContent = new byte[file.ContentLength];
file.InputStream.Read(fileContent, 0, file.ContentLength);
object[] paramaters =
{
file.FileName,
file.FileName,
file.ContentType,
file.ContentLength,
fileContent,
DateTime.Now,
12518
};
db.ExecuteNonQuery("sp_Blob_Insert", paramaters);
}
return RedirectToAction("Index");
}
しかし、以下の View アクション コードを使用してファイルをブラウザーに読み込むと、FileContent フィールドは常に null になります。
public ActionResult View(int id)
{
Database db = DatabaseFactory.CreateDatabase("dbconnstr");
Blob blob = db.ExecuteSprocAccessor<Blob>("sp_Blob_SelectByPkValue", id).Single();
return File(blob.FileContent, blob.FileMimeType, blob.FileName);
}
ただし、フィールド名を具体的にマップすると、次のように機能します。
public ActionResult View(int id)
{
Database db = DatabaseFactory.CreateDatabase("dbconnstr");
IRowMapper<Blob> mapper = MapBuilder<Blob>.MapAllProperties().MapByName(x => x.FileContent).Build();
Blob blob = db.ExecuteSprocAccessor<Blob>("sp_Blob_SelectByPkValue", mapper, id).Single();
return File(blob.FileContent, blob.FileMimeType, blob.FileName);
}
これは ExecuteSprocAccessor() 関数のバグですか? 私は何か間違ったことをしていますか?
お時間をいただきありがとうございます。