假设我的SQL Server 2012数据库中有以下表:
Person
PersonId
FirstName
LastName
Photo
PhotoId
PersonId (fk)
DateTaken
PhotoFileTable
(all the FileTable columns)存储在磁盘上的照片结构是这样的:\myserver\filestreamshare\People\PersonId\Photo1.tif
非常重要的是:磁盘上已经有大量的照片需要添加到数据库中-这就是为什么我认为FileTable会很酷,因为它会自动拾取它们。
因此,我需要做两件事-首先,将照片表关联到PhotoFileTable,这样我就可以获得一个人的所有照片。其次(也是更痛苦的),我想使用Entity Framework5.0来做这件事。
使用edmx设计器,我不能添加包含hierarchyid的表。由于这是主键,因此似乎应该将其用作PhotoId和path_locator ( FileTable层次is )之间的1:1映射。但是我也不能添加Photo表。
这里最好的方法是什么?最后,我想在C#中使用EF对象。理想情况下,它应该看起来像这样:
class Person
List<Photo>
class Photo
Filestream (to lazy load the image from the filesystem to bitmapimage)
Path (?)
or maybe
class Photo
BitmapImage (lazy load)我是不是走错路了?我能从这里到那里吗?有什么想法或建议?
发布于 2012-11-02 17:09:51
也许你可以试试这个。
表:
PhotoTable(
PhotoID uniqueidentifier ROWGUIDCOL NOT NULL,
PhotoImage varbinary(max) FILESTREAM NULL,)
插入:
create procedure spPhotoInsert
@PhotoID uniqueidentifier
,@sPhotoPath nvarchar(max)
,@PhotoImage varbinary(max)
as
begin
select
cast('' as varbinary(max)) PhotoImage
into
#ret1
truncate table #ret1
declare @strSql nvarchar(max) = 'select * from OPENROWSET(BULK '''
+ @sPhotoPath + ''',SINGLE_BLOB) AS PhotoImage'
insert into #ret1 EXEC(@strSql)
insert into
PhotoTable
(
PhotoID
,PhotoImage
)
select
@PhotoID
,PhotoImage
from
#ret1
drop table #ret1
end更新:
create procedure spPhotoUpdate
@PhotoID uniqueidentifier
,@sPhotoPath nvarchar(max)
,@PhotoImage varbinary(max)
as
begin
select
cast('' as varbinary(max)) PhotoImage
into
#ret1
truncate table #ret1
declare @strSql nvarchar(max) = 'select * from OPENROWSET(BULK '''
+ @sPhotoPath + ''',SINGLE_BLOB) AS PhotoImage'
insert into #ret1 EXEC(@strSql)
update
PhotoTable
set
PhotoImage = r.PhotoImage
from
PhotoTable, #ret1 r
where
PhotoID = @PhotoID
drop table #ret1
end删除:
create procedure PhotoDelete
@PhotoID uniqueidentifier
as
begin
delete
PhotoTable
where
PhotoID = @PhotoID
end视图:
CREATE VIEW vPhotoTable
AS
select
PhotoID
,'' as sPhotoPath
,PhotoImage
from
PhotoTable在此之后,可以使用EF对图像进行读写,如下所示:
//InsertPhoto(sPath)
Entities db = new Entities();
vPhoto p = db.vPhotos.CreateObject();
p.PhotoID = Guid.NewGuid();
p.sPhotoPath = sPath;
db.vPhotos.AddObject(p);
db.SaveChanges();
//UpdatePhoto(PhotoID,sPath):
Entities db = new Entities();
vPhoto p = db.vPhotos.Where(x => x.PhotoID == PhotoID).Single();
p.sPhotoPath = sPath;
db.ObjectStateManager.ChangeObjectState(p, EntityState.Modified);
db.SaveChanges();
//DeletePhoto(PhotoID):
Entities db = new Entities();
vPhoto p = db.vPhotos.Where(x => x.PhotoID == PhotoID).Single();
db.vPhotos.DeleteObject(p);
db.SaveChanges();https://stackoverflow.com/questions/13181645
复制相似问题