Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As some mentioned in the comments to Mortezas answer there is a CreateIndex/DropIndex method if you use migrations.</p> <p>But if you are in "debug"/development mode and is changing the schema all the time and are recreating the database every time you can use the example mentioned in Morteza answer.</p> <p>To make it a little easier, I have written a very simple extension method to make it strongly typed, as inspiration that I want to share with anyone who reads this question and maybe would like this approach aswell. Just change it to fit your needs and way of naming indexes.</p> <pre><code>You use it like this: context.Database.CreateUniqueIndex&lt;User&gt;(x =&gt; x.Name); </code></pre> <p>.</p> <pre><code> public static void CreateUniqueIndex&lt;TModel&gt;(this Database database, Expression&lt;Func&lt;TModel, object&gt;&gt; expression) { if (database == null) throw new ArgumentNullException("database"); // Assumes singular table name matching the name of the Model type var tableName = typeof(TModel).Name; var columnName = GetLambdaExpressionName(expression.Body); var indexName = string.Format("IX_{0}_{1}", tableName, columnName); var createIndexSql = string.Format("CREATE UNIQUE INDEX {0} ON {1} ({2})", indexName, tableName, columnName); database.ExecuteSqlCommand(createIndexSql); } public static string GetLambdaExpressionName(Expression expression) { MemberExpression memberExp = expression as MemberExpression; if (memberExp == null) { // Check if it is an UnaryExpression and unwrap it var unaryExp = expression as UnaryExpression; if (unaryExp != null) memberExp = unaryExp.Operand as MemberExpression; } if (memberExp == null) throw new ArgumentException("Cannot get name from expression", "expression"); return memberExp.Member.Name; } </code></pre> <hr> <p>Update: From version 6.1 and onwards there is an [Index] attribute available.</p> <p>For more info, see <a href="http://msdn.microsoft.com/en-US/data/jj591583#Index" rel="nofollow">http://msdn.microsoft.com/en-US/data/jj591583#Index</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload