3

我应该如何映射具有可为空字段的对象?我想我必须将可为空的字段转换为不可为空的版本,这就是我偶然发现的那一步。

映射可空属性的正确方法是什么?

public class Visit {
    public string Id { get; set; }
    public int? MediaSourceId { get; set; }
}

public class MapReduceResult
{
    public string VisitId { get; set; }
    public int MediaSourceId { get; set; }
    public string Version { get; set; }
    public int Count { get; set; }
}

AddMap<Visit>(
    visits =>
    from visit in visits
    select new
        {
            VisitId = visit.Id,
            MediaSourceId =
                (visit.MediaSourceId.HasValue)
                ? visit.MediaSourceId
                : UNUSED_MEDIASOURCE_ID,
            Version = (string) null,
            Count = 1
        });

这不行!实际上; 此 Map 被完全忽略,而其他 Map 工作正常,并且它们最终按预期减少。

谢谢你帮助我!

下面是一个新添加的测试用例,失败并显示“无法将 <null> 分配给匿名类型属性”。我应该如何以最少的痛苦让这架飞机飞起来?

[TestFixture]
public class MyIndexTest
{
    private IDocumentStore _documentStore;

    [SetUp]
    public void SetUp()
    {
        _documentStore = new EmbeddableDocumentStore {RunInMemory = true}.Initialize();
        _documentStore.DatabaseCommands.DisableAllCaching();

        IndexCreation.CreateIndexes(typeof (MyIndex).Assembly, _documentStore);
    }

    [TearDown]
    public void TearDown()
    {
        _documentStore.Dispose();
    }

    [Test]
    public void ShouldWork()
    {
        InitData();

        IList<MyIndex.MapReduceResult> mapReduceResults = null;
        using (var session = _documentStore.OpenSession())
        {
            mapReduceResults =
                session.Query<MyIndex.MapReduceResult>(
                    MyIndex.INDEX_NAME)
                    .Customize(x => x.WaitForNonStaleResults()).ToArray();
        }
        Assert.That(mapReduceResults.Count, Is.EqualTo(1));
    }

    private void InitData()
    {
        var visitOne = new Visit
                           {
                               Id = "visits/64",
                               MetaData = new MetaData {CreatedDate = new DateTime(1975, 8, 6, 0, 14, 0)},
                               MediaSourceId = 1,
                           };
        var visitPageVersionOne = new VisitPageVersion
                                      {
                                          Id = "VisitPageVersions/123",
                                          MetaData = new MetaData {CreatedDate = new DateTime(1975, 8, 6, 0, 14, 0)},
                                          VisitId = "visits/64",
                                          Version = "1"
                                      };

        using (var session = _documentStore.OpenSession())
        {
            session.Store(visitOne);
            session.Store(visitPageVersionOne);
            session.SaveChanges();
        }
    }

    public class MyIndex :
        AbstractMultiMapIndexCreationTask
            <MyIndex.MapReduceResult>
    {
        public const string INDEX_NAME = "MyIndex";

        public override string IndexName
        {
            get { return INDEX_NAME; }
        }

        public class MapReduceResult
        {
            public string VisitId { get; set; }
            public int? MediaSourceId { get; set; }
            public string Version { get; set; }
            public int Count { get; set; }
        }

        public MyIndex()
        {
            AddMap<Visit>(
                visits =>
                from visit in visits
                select new
                           {
                               VisitId = visit.Id,
                               MediaSourceId = (int?) visit.MediaSourceId,
                               Version = (string) null,
                               Count = 1
                           });
            AddMap<VisitPageVersion>(
                visitPageVersions =>
                from visitPageVersion in visitPageVersions
                select new
                           {
                               VisitId = visitPageVersion.VisitId,
                               MediaSourceId = (int?) null,
                               Version = visitPageVersion.Version,
                               Count = 0
                           });
            Reduce =
                results =>
                from result in results
                group result by result.VisitId
                into g
                select
                    new
                        {
                            VisitId = g.Key,
                            MediaSourceId = (int?) g.Select(x => x.MediaSourceId).FirstOrDefault(),
                            Version = g.Select(x => x.Version).FirstOrDefault(),
                            Count = g.Sum(x => x.Count)
                        };
        }
    }
}
4

1 回答 1

0

您不需要做任何事情来给予可空值特殊待遇。RavenDB 已经解决了这个问题。

于 2012-08-22T07:18:39.243 回答