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();
            }

    源码地址

  • 相关阅读:
    要搜索内容
    .net core 过滤器
    C# => 写法
    js 数组的forEach 函数
    .net core 下载文件 其他格式
    win10 1903 更改文字大小
    fetch 写法
    C# 匿名对象 增加属性
    ping —— 虚拟机
    selenium验证车贷计算器算法
  • 原文地址:https://www.cnblogs.com/winhu/p/MongoDB_EnsureIndex.html
Copyright © 2011-2022 走看看