c# - Entity Framework 5.0 和 FileTable

标签 c# sql entity-framework sql-server-2012 filetable

假设我的 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 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)

我的处理方式是错误的吗?我可以从这里到那里吗?想法或建议?

最佳答案

也许你可以试试这个。

表:

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

以及 View :

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

关于c# - Entity Framework 5.0 和 FileTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13181645/

相关文章:

asp.net - 我有一个非常非常慢的 EF 查询。我该如何调试呢?

c# - 在 Entity Framework 中,如何在没有枚举所有可能 DbSet 的 switch 语句的情况下将通用实体添加到其相应的 DbSet?

c# - 透明选项卡控件显示为白色

c# - 我应该等待使用 TaskCompletionSource 的同步 Task<T> 吗?

mysql - 查找不存在的最低 ID

sql - oracle sql select 与员工

c# - 碰撞声

c# - 在 C# 中创建气球工具提示

sql - 警告 : pg_query(): Query failed: ERROR: syntax error at or near "WHERE"

c# - 组合来自不同表的多个 Select 语句