Save a file in SQL Server 2008 database with Entity Framework

别等时光非礼了梦想. 提交于 2019-12-10 21:54:16

问题


How can I save a file in SQL Server 2008 database with Entity Framework?

I want to use FileStream in SQL Server 2008.


回答1:


I don't see why this wouldn't work - filestream columns are just exposed as varbinary(MAX) so you should be able to set the column using Entity Framework:

Sample from here:

Files newFile = new Files();
newFile.FileID = Guid.NewGuid();
newFile.FileContents = System.IO.File.ReadAllBytes("TextFile1.txt");
ctx.AddObject("Files", newFile);
ctx.SaveChanges();



回答2:


Entity framework doesn't have support for SQL server specific features like filestream. You can't do it with EF you must fall back to ADO.NET.

Edit:

Undeleting my answer because I just tested how EF handles FILESTREAM columns. EF indeed works with FILESTREAM column so my answer is not fully correct and @BrokenGlass provided the solution. The problem is that EF ignores FILESTREAM keyword and doesn't really use streaming when working with FILESTREAM column. That column is handled as any other binary column.



来源:https://stackoverflow.com/questions/5370351/save-a-file-in-sql-server-2008-database-with-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!