zoukankan      html  css  js  c++  java
  • MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 三

    本次改动的主要内容是实现MongoDB.Repository在MongoDB中建立索引。

    建立索引主要使用MongoDB的官方驱动中EnsureIndex方法。

    在MongoDB.Repository中建立一个BsonIndexAttribute,用以标识需要建立索引的属性。

        /// <summary>
        /// Indicates that this field or property should be index.
        /// </summary>
        [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
        public class BsonIndexAttribute : Attribute
        {
        }

    BsonIndexAttribute的机制是在注册全部实体类型后,在统一建立索引,所以需要在MongoDBRepository.RegisterMongoDBContext(new TestDBContext())之后进行MongoDBRepository.RegisterMongoIndex()操作。

    编码演示如下:

        public class Student : Entity
        {
            [BsonIndex]
            public string Name { get; set; }
            public int Age { get; set; }
        }
            [TestFixtureSetUp]
            public void Setup()
            {
                MongoDBRepository.RegisterMongoDBContext(new TestDBContext());
                MongoDBRepository.RegisterMongoIndex();
            }

    源码地址

  • 相关阅读:
    BZOJ4036 HAOI2015按位或(概率期望+容斥原理)
    洛谷p2661信息传递题解
    洛谷P1434滑雪题解及记忆化搜索的基本步骤
    二分图最大匹配
    线段树

    图论基本算法
    并查集
    RMQ--ST表
    矩阵快速幂和矩阵乘法
  • 原文地址:https://www.cnblogs.com/winhu/p/MongoDB_EnsureIndex.html
Copyright © 2011-2022 走看看