所以说我的SQL Server 2012 DB中有以下表格:
Person
PersonId
FirstName
LastName
Photo
PhotoId
PersonId (fk)
DateTaken
PhotoFileTable
(all the FileTable columns)
存储在磁盘上的照片的结构如下:
\\ myserver \ filestreamshare \ People \ PersonId \ Photo1.tif
并且非常重要:磁盘上已经有大量照片需要添加到数据库中,这就是为什么我认为FileTable会很酷,因为它会自动将它们拾取。
因此,我需要做两件事-首先,将Photo表与PhotoFileTable关联,以便我可以获取一个人的所有照片。其次(更痛苦的是),我想使用Entity Framework 5.0来做到这一点。
使用edmx设计器,我无法添加包含architectureid的表。由于这是主键,因此它似乎应该用作PhotoId和path_locator(FileTable层次结构ID)之间的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
和视图:
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/