Is there a workaround to enable FILESTREAM in the code first approach of entity framework

别来无恙 提交于 2020-04-30 07:45:28

问题


After much of googling I have come to three conclusion that until MVC 4 ( entity framework), that FILESTREAM (of Sql server) is not supported directly.

Is there a work around for the same?


回答1:


Unfortunatelly, there is no equivalent of FILESTREAM in EF 6, that could be used with Code First approach. There is a workaround I found today, but haven't tried myself yet, though:

http://ignoringthevoices.blogspot.co.uk/2014/01/working-with-entity-framework-code.html

Basicaly, you need to use database first and create column of VARBINARY(max) type. Then tell EF to ignore it when auto-mapping and use custom sql query to read/write the content.

Hope it helped.




回答2:


The best way for using FileStream Near Entity Framework Code first is things like this: in your model has one field with byte[] type and in up method of migration write this code :

 DropColumn("dbo.Judges", "Photo");
 Sql("alter table [dbo].[Judges] add [PhotoTemp] varbinary(max) FILESTREAM not null");
 RenameColumn("dbo.Judges", "PhotoTemp", "Photo");
 Sql("alter table [dbo].[Judges] add constraint [DF_Judges_Photo] default(0x) for [Photo]");


来源:https://stackoverflow.com/questions/19900028/is-there-a-workaround-to-enable-filestream-in-the-code-first-approach-of-entity

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