Just a simple question: Is it possible to create indexes programmatically somehow?

(Would like to add indexes during runtime, ie add directly to domain config before build)

This is my model (kind of, anyway, I've simplified it a bit, but I think it should behave irrationally :) )

EBase : Entity

E<T1, T2> : EBase //T1 and T2 are restricted to entities of a certain base class, if that's relevant

Placing [HierachyRoot] on EBase will make index Foo to not appear in DB, however, placing [HierachyRoot] on E<t1, t2=""> will work as expected. I'm not sure if I have to set the inheritance schema to ConcreteTable for it to work as expected, but as far as I can remember it won't affect the result when placing the [HierarchyRoot] on EBase

(In DB I have tables for every combination of T1 and T2 that's relevant for my application, and it's those tables I'm interested in.)

asked Sep 09 '13 at 05:11

Onkelborg's gravatar image


edited Sep 17 '13 at 17:03

One Answer:

Hello Onkelborg,

index configuration could be accomlished by applying [Index] attribute on your persistent types. Or using domain builder API. Most like you'll need a second options in your case.

To define index you'll need to implement IModule interface in assembly with your persistent types. Then in method OnDefinitionsBuilt call DefineIndex on TypeDef objects you need to have custom index.

answered Sep 10 '13 at 03:32

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov

Ah, I didn't know about that option, I'll check it out :) Thanks :)

(Sep 10 '13 at 03:34) Onkelborg Onkelborg's gravatar image

I can't get this to work. I've create a class inheriting from Module, and I've got an override on OnDefinitionsBuilt. I'm adding an index to a set of TypeDef, but the indexes doesn't end up in the db. The info-log doesn't mention the indexes either. However, if I intentionally misspell a field name or similar then exceptions will be thrown. I know my code is executed.

Do you have any sample? (I've tried quite a few variations now but I don't get it :/ )

(Sep 10 '13 at 06:33) Onkelborg Onkelborg's gravatar image

Here is a working example from our test suite:

  indexDef = types["Author"].DefineIndex("IX_PENNAME");
  indexDef.MappingName = "IX_PENNAME";
  indexDef.KeyFields.Add("PenName", Direction.Negative);

  indexDef = types["Book"].DefineIndex("IX_Title");
(Sep 10 '13 at 06:47) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

Looks quite similar I think, I have to investigate further

(Sep 10 '13 at 07:26) Onkelborg Onkelborg's gravatar image

I think I have found a bug. If I define an index on a regular entity then everything is working. If I define an index on a generic entity (ex: MyGenericEntity<somethingelse>) then I won't get any index in the database, but no exception either.

(Sep 15 '13 at 09:44) Onkelborg Onkelborg's gravatar image

Hm, this doesn't seem to be a bug just concerning DefineIndex, it's bigger - if I define indexes directly on MyEntity<t> using IndexAttribute then those indexes won't show up

(Sep 15 '13 at 13:38) Onkelborg Onkelborg's gravatar image

Interesting - if I set HierarchyRoot directly on MyEntity<t> instead of MyBaseEntity then the indexes will be created - is this a bug due to inheritance?

(Sep 15 '13 at 13:54) Onkelborg Onkelborg's gravatar image

Hi Onkelborg, thank you for your investigation. Could you provide more detailed information on how to reproduce this bug please?

(Sep 16 '13 at 03:52) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

I've added a simplified model of my model to the question, I think I've kept the relevant details

(Sep 17 '13 at 17:04) Onkelborg Onkelborg's gravatar image

Seems you're using "automatic generic instantiation" feature. Are your indexes missing for all generic instances or for only for some of them?

(Sep 18 '13 at 10:47) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

Actually, I've added the types manually (.MakeGenericType()). I don't really remember/know, but I think all generic instances missed their indexes

(Sep 24 '13 at 10:29) Onkelborg Onkelborg's gravatar image

Hi Onkelborg. I tried to reproduce the issue, but failed. Indexes are built correctly in my tests. I guess, I'm missing some details. Could you send the model and domain build routine to support@x-tensive.com? Thanks.

(Oct 04 '13 at 02:35) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA