2012-11-01 58 views
2

所以说,我在我的SQL Server 2012数据库下表:实体框架5.0的FileTable

Person 
    PersonId 
    FirstName 
    LastName 

Photo 
    PhotoId 
    PersonId (fk) 
    DateTaken 

PhotoFileTable 
    (all the FileTable columns) 

,并存储在磁盘上的照片都是结构是这样的: \\ MYSERVER \ filestreamshare \人\ PERSONID \ Photo1.tif

而且非常重要:在磁盘上有一张TON照片需要添加到数据库 - 这就是为什么我认为FileTable会很酷,因为它会自动拾取它们。

所以我需要做2件事情 - 首先,将照片表与PhotoFileTable相关联,以便我可以获取一个人的所有照片。第二个(也更痛苦)我想用Entity Framework 5.0来做到这一点。

使用edmx设计器,我无法添加包含hierarchyid的表。由于这是主键,它似乎应该用作PhotoId和path_locator(FileTable的hierarchyid)之间的1:1映射。但是,我无法添加照片表。

这里最好的办法是什么?在这一天结束时,我想让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) 

我该怎么做这个错误的方式呢?我可以从这里到达吗?想法或建议?

回答

2

也许你可以试试这个。

表:

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(); 
+0

好的,谢谢!除了我的数据库中的表是SQL 2012 FileTable,而不是FileStream。所以问题仍然是如何在DB端将这两件事联系在一起。虽然SP方法可能是正确的... – Nicros

+0

我同意,我很抱歉,但我没有意识到我们正在谈论SQL 2012。 –