图像标记数据库

时间:2015-05-22 20:43:44

标签: c# database-design linq-to-entities

我需要为学校项目设计图像标记数据库。

该网站将包含图片,标签和用户。 Evey Image标记有一个或多个标签(标签类似于:夏天,海滩,Tyoko等),但我还需要跟踪谁添加和删除哪些标签的历史记录。

我想出的解决方案是拥有一个像这样的TagHistory表:

public class TagHistory
{
    public virtual int TagHistoryId { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual User TaggedBy{ get; set; }
    public virtual Image Image { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

因此,图像上的当前标记只是图像最新的TagHistory条目。

这导致搜索问题,因为只有最新的TagHistory条目计数我首先必须获取每个图像的所有当前TagHistories,然后然后执行过滤。这就是我想要搜索包含特定标签的所有图像的原因:

var curTagHis = from tagHistory in ctx.TagHistories
                group tagHistory by tagHistory.Image
                into groups
                select groups.OrderByDescending(th => th.Date).FirstOrDefault();

var images = from tagHistory in curTagHis 
             where tagHistory.Tags.Any(t => t.TagID == tag.TagID)
             select tagHistory.Image;

我想,随着我添加更多功能,它只会变得更糟。

我在想,也许我需要想出一个可以分割图像的设计。当前标记,以及标记到单独实体中的历史记录。这是一个很好的方向还是有其他方法可以做到这一点?我猜这已经是一个已经解决的问题了。

1 个答案:

答案 0 :(得分:0)

我会分开它。最终用户不会根据3周前图片的标签来搜索图片,而是根据目前标签的标签。我会选择这样的东西:

create table images
(
    id int primary key,
    title varchar(255) not null,
    file_location varchar(255) not null
);

create table tags
(
    id int primary key,
    title varchar(255) not null
);

create table images_tags
(
    image_id int not null,
    tag_id int not null,
    primary key (image_id, tag_id)
);

create table images_tags_log
(
    image_id int not null,
    tag_id int not null,
    created_by varchar(255) not null,
    date_create datetime not null,
    deleted_by varchar(255),
    date_delete datetime null,
    primary key (image_id, tag_id, date_create)
);

这允许轻松提取当前图像的标签,但如果您想了解更多信息,可以查看image_tags_log表。

相关问题