【问题标题】:Can't seem to expand an Image Navigation property with Breeze JS似乎无法使用 Breeze JS 扩展图像导航属性
【发布时间】:2013-07-19 18:12:47
【问题描述】:

我正在使用 MVC、实体框架、Durandal 和 Breeze JS。我有一个看起来像这样的用户(简化):

public class User : EntityBase<Guid>, IAggregateRoot
{
    public Guid Id { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [ForeignKey("UserImage")]
    public virtual Guid? ImageId { get; set; }

    public virtual UserImage UserImage { get; set; }
}

UserImage 类看起来像这样。我知道我应该限制图像的大小。 (也许这就是问题所在?):

public class UserImage
{
    public Guid Id { get; set; }

    [MaxLength]
    public byte[] Image { get; set; }        
}

我在服务器上有一个api函数来获取当前用户:

public IQueryable<User> GetCurrentUser()
    {
        IPrincipal principal = HttpContext.Current.User;
        var users = _uow.Users.FindBy(u => u.UserName.Equals(principal.Identity.Name));
        if (!users.Any())
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.Unauthorized));
        }

        return users;
    }

客户端上的两个调用获取当前用户。第一个是在shell中:

function loadCurrentUser() {
        return uow.CurrentUser.all().then(function (newUser) {
            log('Welcome to the Site ' + newUser[0].FullName() + '!', newUser[0], true);
            config.CurrentUser(newUser[0]);
            return true;
        });
    }

第二个在 ManageUser 视图模型中:

function activate() {
                    return uow.CurrentUser.all(['UserImage']).then(function (user) {
                        self.CurrentUser(user[0]);
                        return $.when(init()).then(boot());
                    }).fail(function() {
                        return router.activate('accounts/login');
                    });
                }

现在我可以将图像加载到 ManageUser 页面并保存并在提琴手中显示 ImageId 和 Image 正在发送到服务器。然后我检查了 BeforeSaveEntity 拦截并显示两个实体正在保存。

  1. 已设置 ImageId 的更新用户
  2. 新用户图片

数据在数据库中也是可见的。现在,当我刷新管理用户页面时,我可以在 fiddler 中看到两个 GetCurrentUser 调用。

  1. 从 shell 调用中,我可以看到正在返回用户并设置了 ImageId,但没有发送 UserImage,因为没有展开查询。
  2. 从“管理用户”调用中,我看到返回了用户,但只发送了 ImageId,并且从 JSON 中省略了 Image 对象。

有人遇到过这个图片问题吗?我所有的其他扩展似乎都正常工作。有没有人有任何关于使用微风仅保存图像的文件路径并可能使用 Windows azure 进行媒体存储的示例?

【问题讨论】:

    标签: entity-framework-5 breeze durandal


    【解决方案1】:

    我知道这可能无法回答您的问题,但我建议不要将字节数组发送到客户端,而是在服务器端设置一个图像处理程序,该处理程序将 ImageId 作为参数,然后返回相关图像内容类型集。这方面的一个例子可以在here找到。

    通过使用这种方法,您可以使用 img 标记从 HTML 中引用您的图像,并将源设置为具有相关 ImageId 的图像处理程序。

    使用敲除进行数据绑定的示例是:

    <a data-bind="attr: {href: '/Image/' + User.ImageId()}"></a>
    

    这种方法使您能够轻松地在服务器和客户端上添加缓存,从而提高性能。它还消除了在客户端将字节数组转换为图像的需要,这可能会也可能不会很痛苦。

    编辑:

    保存托管用户时,将图像发布到 ImageHandler 上的上传操作(查看this article)。此操作必须返回图像的新 ID。收到新 ID 后,在客户端更新 User.ImageId 并在微风中调用 SaveChanges。

    【讨论】:

    • 感谢您的回复。我将在本周末进一步研究这种方法。我主要是尝试这种方式来测试微风的力量。因此,按照您的方法,如果我要在页面上管理用户,添加图像并保存,我会调用微风SaveChanges 存储托管用户UserImage 具有假定的href '/Image/ImageId',然后继续将图像上传到图像控制器?
    • 谢谢,这绝对是一个更好的方法。
    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多