diff --git a/src/MongoFramework/Attributes/IndexAttribute.cs b/src/MongoFramework/Attributes/IndexAttribute.cs index 552af0d4..625c02c4 100644 --- a/src/MongoFramework/Attributes/IndexAttribute.cs +++ b/src/MongoFramework/Attributes/IndexAttribute.cs @@ -2,7 +2,7 @@ namespace MongoFramework.Attributes { - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class IndexAttribute : Attribute { /// diff --git a/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs b/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs index f76a7ca0..ad025fea 100644 --- a/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs +++ b/tests/MongoFramework.Tests/Infrastructure/Indexing/IndexModelBuilderTests.cs @@ -92,6 +92,15 @@ public class TenantUniqueConstraintModel : IHaveTenantId public string UniqueIndex { get; set; } } + public class MultipleIndexesOnSinglePropertyModel + { + [Index("MyMainIndex", IndexSortOrder.Ascending)] + [Index("MyCompoundIndex", IndexSortOrder.Ascending, IndexPriority = 1)] + public string MyCustomField { get; set; } + + [Index("MyCompoundIndex", IndexSortOrder.Descending, IndexPriority = 2)] + public string OtherField { get; set; } + } [TestMethod] public void IndexNaming() @@ -204,6 +213,16 @@ public void AppliesTenantConstraint() Assert.AreEqual("{ \"TenantId\" : 1, \"UniqueIndex\" : 1 }", indexBsonDocument); } + [TestMethod] + public void MultipleIndexesOnSingleProperty() + { + var indexModel = IndexModelBuilder.BuildModel(); + + Assert.AreEqual(2, indexModel.Count()); + var results = indexModel.Select(i => i.Keys.Render(null, null)); + Assert.IsTrue(results.Any(e => e.Contains("MyCustomField") && e.ElementCount == 1)); + Assert.IsTrue(results.Any(e => e.Contains("MyCustomField") && e.Contains("OtherField") && e.ElementCount == 2)); + } } } \ No newline at end of file