A week ago I asked on how to propagate a parameter for joins in a ORM. The suggested solution looks as promising -- using NHibernate filters. Here is what I'm doing:
Filter class:
public class VersionFilter : FilterDefinition
{
public static readonly string Name = typeof(VersionFilter).AssemblyQualifiedName;
public static readonly string Parameter = "AsOf";
private static readonly string SqlParameter = ":" + Parameter;
private static readonly string Condition
= " ISNULL(" + SqlParameter + ") AND ( ISNULL(ValidTo) )" +
" OR ( ValidFrom < " + SqlParameter + " AND " + SqlParameter + " <= ValidTo )"
;
private static readonly IDictionary<string, IType> ParameterTypes =
new Dictionary<string, IType>
{
{Parameter, NHibernateUtil.Int64}
}
;
public VersionFilter(): base(Name, Condition, ParameterTypes, true)
{
}
}
In Main:
public class Program
{
static void Main(string[] args)
{
log.Info("VersionedDataModel1.Program::start");
try
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddFilterDefinition(new VersionFilter());
var types = DomainClasses(typeof(Program).Assembly, "VersionedDataModel1.Domain");
foreach (Type type in types)
{
cfg.AddInputStream(HbmSerializer.Default.Serialize(type));
}
var sessionFactory = cfg.BuildSessionFactory();
{
var schema = new SchemaExport(cfg);
schema.Drop(false, true);
schema.Execute(false, true, false);
//new SchemaExport(cfg).Drop(false, true);
//new SchemaExport(cfg).Execute(false, true, false);
//new SchemaUpdate(cfg).Execute(false, true);
}
}
}
}
One of Entity classes:
[Class]
public class Store : BaseEntity
{
private ICollection<Product> _products = new List<Product>();
private ICollection<Employee> _staff = new List<Employee>();
[Property(NotNull = true)]
public virtual string Name { get; set; }
[Set(0, Table = "ProductsInStore", Lazy = CollectionLazy.False, Cascade = "none")]
[Key(1, Column = "StoreId")]
[ManyToMany(2, Column = "ProductId", ClassType = typeof(Product))]
public virtual ICollection<Product> Products { get { return _products; } set { _products = value; } }
[Key(1, Column = "StoreId")]
[OneToMany(2, ClassType = typeof(Employee))]
public virtual ICollection<Employee> Staff { get { return _staff; } set { _staff = value; } }
public virtual void AddProduct(Product product)
{
product.StoresStockedIn.Add(this);
Products.Add(product);
}
public virtual void AddEmployee(Employee employee)
{
employee.Store = this;
Staff.Add(employee);
}
}
To query data I do the following:
public IEnumerable<Product> QueryAllProducts(long? asOf)
{
using (var session = SessionFactory.OpenSession())
{
session.EnableFilter(VersionFilter.Name).SetParameter(VersionFilter.Parameter, asOf);
return session.QueryOver<Product>().List<Product>();
}
}
In NHibernate debug log I see the following. When I construct SessionFactory. I see the following error, that I'm not sure how to interpret:
2015-04-30 13:53:32,398 [1] ERROR NHibernate.Cfg.Configuration - filter-def for filter named 'VersionedDataModel1.VersionFilter, VersionedDataModel1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' was never used to filter classes nor collections.
This may result in unexpected behavior during queries
Further on in log I see no reference to the filter and neither my Product collection is filtered. May be I'm missing the concept.
My idea is to apply this filter (VersionFilter) for the whole session so that all entities that are queried will have this addidional WHERE ... condition to be used.
Hence is the question -- what am I missing with Filter/FilterDefinition ?
Thanks!
You already registered the filter to nhibernate, now you have to call a reference in your mapping class or collections.
I have never used mapping by attributes but i asume you need to use NHibernate.Mapping.Attributes.Filter to relate the filter with the entity (class level)
Or dynamic like this:
var filterParametersType = new Dictionary<string, Itype>(1);
filterParametersType.Add("current", NhibernateUtil.Enum(typeof(ContextType)));
cfg.AddFilterDefinition(new FilterDefinition("contextFilter", ":current = Context", filterParametersType));
foreach (var mapping in cfg.ClassMappings)
{
if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))
{
mapping.AddFilter("contextFilter", ":current = Context");
}
}
You can read more here in nhibernate documentation.
I'm running EF 4.2 CF and want to create indexes on certain columns in my POCO objects.
As an example lets say we have this employee class:
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
We often do searches for employees by their EmployeeCode and since there are a lot of employees it would be nice to have that indexed for performance reasons.
Can we do this with fluent api somehow? or perhaps data annotations?
I know it is possible to execute sql commands something like this:
context.Database.ExecuteSqlCommand("CREATE INDEX IX_NAME ON ...");
I would very much like to avoid raw SQL like that.
i know this does not exist but looking for something along those lines:
class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
internal EmployeeConfiguration()
{
this.HasIndex(e => e.EmployeeCode)
.HasIndex(e => e.FirstName)
.HasIndex(e => e.LastName);
}
}
or maybe using System.ComponentModel.DataAnnotations the POCO could look like this (again i know this does not exist):
public class Employee
{
public int EmployeeID { get; set; }
[Indexed]
public string EmployeeCode { get; set; }
[Indexed]
public string FirstName { get; set; }
[Indexed]
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
Anyone have any ideas on how to do this, or if there are any plans to implement a way to do this, the code first way?
UPDATE: As mentioned in the answer by Robba, this feature is implemented in EF version 6.1
After Migrations was introduced in EF 4.3 you can now add indexes when modifying or creating a table. Here is an excerpt from the EF 4.3 Code-Based Migrations Walkthrough from the ADO.NET team blog
namespace MigrationsCodeDemo.Migrations
{
using System.Data.Entity.Migrations;
public partial class AddPostClass : DbMigration
{
public override void Up()
{
CreateTable(
"Posts",
c => new
{
PostId = c.Int(nullable: false, identity: true),
Title = c.String(maxLength: 200),
Content = c.String(),
BlogId = c.Int(nullable: false),
})
.PrimaryKey(t => t.PostId)
.ForeignKey("Blogs", t => t.BlogId, cascadeDelete: true)
.Index(t => t.BlogId)
.Index(p => p.Title, unique: true);
AddColumn("Blogs", "Rating", c => c.Int(nullable: false, defaultValue: 3));
}
public override void Down()
{
DropIndex("Posts", new[] { "BlogId" });
DropForeignKey("Posts", "BlogId", "Blogs");
DropColumn("Blogs", "Rating");
DropTable("Posts");
}
}
}
This is a nice strongly typed way to add the indexes, which was what i was looking for when i first posted the question.
You could create an attribute called indexed (as you suggested), which is then picked up in a custom initializer.
I created the following attribute:
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public class IndexAttribute : Attribute
{
public IndexAttribute(bool isUnique = false, bool isClustered = false, SortOrder sortOrder = SortOrder.Ascending)
{
IsUnique = isUnique;
IsClustered = isClustered;
SortOrder = sortOrder == SortOrder.Unspecified ? SortOrder.Ascending : sortOrder;
}
public bool IsUnique { get; private set; }
public bool IsClustered { get; private set; }
public SortOrder SortOrder { get; private set; }
//public string Where { get; private set; }
}
I then created a custom initializer which got a list of the table names created for the entities in my context. I have two base classes which all my entities inherit, so I did the following to get the table names:
var baseEF = typeof (BaseEFEntity);
var baseLink = typeof (BaseLinkTable);
var types =
AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(
baseEF.IsAssignableFrom).Union(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(
s => s.GetTypes()).Where(
baseLink.IsAssignableFrom));
var sqlScript = context.ObjectContext.CreateDatabaseScript();
foreach (var type in types)
{
var table = (TableAttribute) type.GetCustomAttributes(typeof (TableAttribute), true).FirstOrDefault();
var tableName = (table != null ? table.Name : null) ?? Pluralizer.Pluralize(type.Name);
I then found all the properties on each entity that have this attribute and then execute a SQL command to generate the index on each property. Sweet!
//Check that a table exists
if (sqlScript.ToLower().Contains(string.Format(CREATETABLELOOKUP, tableName.ToLower())))
{
//indexes
var indexAttrib = typeof (IndexAttribute);
properties = type.GetProperties().Where(prop => Attribute.IsDefined(prop, indexAttrib));
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(indexAttrib, true).ToList();
foreach (IndexAttribute index in attributes)
{
var indexName = string.Format(INDEXNAMEFORMAT, tableName, property.Name,
attributes.Count > 1
? UNDERSCORE + (attributes.IndexOf(index) + 1)
: string.Empty);
try
{
context.ObjectContext.ExecuteStoreCommand(
string.Format(INDEX_STRING, indexName,
tableName,
property.Name,
index.IsUnique ? UNIQUE : string.Empty,
index.IsClustered ? CLUSTERED : NONCLUSTERED,
index.SortOrder == SortOrder.Ascending ? ASC : DESC));
}
catch (Exception)
{
}
}
}
I even went on to add class based indexes (which could have multiple columns) , unique constraints and default constraints all in the same way. Whats also really nice is that if you put these attributes on an inherited class the index or constraint gets applied to all the classes (tables) that inherit it.
BTW the pluralizer helper contains the following:
public static class Pluralizer
{
private static object _pluralizer;
private static MethodInfo _pluralizationMethod;
public static string Pluralize(string word)
{
CreatePluralizer();
return (string) _pluralizationMethod.Invoke(_pluralizer, new object[] {word});
}
public static void CreatePluralizer()
{
if (_pluralizer == null)
{
var aseembly = typeof (DbContext).Assembly;
var type =
aseembly.GetType(
"System.Data.Entity.ModelConfiguration.Design.PluralizationServices.EnglishPluralizationService");
_pluralizer = Activator.CreateInstance(type, true);
_pluralizationMethod = _pluralizer.GetType().GetMethod("Pluralize");
}
}
}
To build on frozen's response, you can hand code it into a migration yourself.
First, go to the Package Manager Console and create a new migration with add-migration, then give it a name. A blank migration will appear. Stick this in:
public override void Up()
{
CreateIndex("TableName", "ColumnName");
}
public override void Down()
{
DropIndex("TableName",new[] {"ColumnName"});
}
Note that if you're using a string field it needs to be capped to a length of 450 chars as well.
I've also looked into this recently and found no other way, so I settled with creating indexes when seeding the database:
public class MyDBInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
private MyContext _Context;
protected override void Seed(MyContext context)
{
base.Seed(context);
_Context = context;
// We create database indexes
CreateIndex("FieldName", typeof(ClassName));
context.SaveChanges();
}
private void CreateIndex(string field, Type table)
{
_Context.Database.ExecuteSqlCommand(String.Format("CREATE INDEX IX_{0} ON {1} ({0})", field, table.Name));
}
}
Note that in Entity Framework 6.1 (currently in beta) will support the IndexAttribute to annotate the index properties which will automatically result in a (unique) index in your Code First Migrations.
For anyone using Entity Framework 6.1+, you can do the following with fluent api:
modelBuilder
.Entity<Department>()
.Property(t => t.Name)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute()));
Read more in the documentation.
Well i found a solution online and adapted it to fit my needs here it is:
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public class IndexAttribute : Attribute
{
public IndexAttribute(string name, bool unique = false)
{
this.Name = name;
this.IsUnique = unique;
}
public string Name { get; private set; }
public bool IsUnique { get; private set; }
}
public class IndexInitializer<T> : IDatabaseInitializer<T> where T : DbContext
{
private const string CreateIndexQueryTemplate = "CREATE {unique} INDEX {indexName} ON {tableName} ({columnName});";
public void InitializeDatabase(T context)
{
const BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance;
Dictionary<IndexAttribute, List<string>> indexes = new Dictionary<IndexAttribute, List<string>>();
string query = string.Empty;
foreach (var dataSetProperty in typeof(T).GetProperties(PublicInstance).Where(p => p.PropertyType.Name == typeof(DbSet<>).Name))
{
var entityType = dataSetProperty.PropertyType.GetGenericArguments().Single();
TableAttribute[] tableAttributes = (TableAttribute[])entityType.GetCustomAttributes(typeof(TableAttribute), false);
indexes.Clear();
string tableName = tableAttributes.Length != 0 ? tableAttributes[0].Name : dataSetProperty.Name;
foreach (PropertyInfo property in entityType.GetProperties(PublicInstance))
{
IndexAttribute[] indexAttributes = (IndexAttribute[])property.GetCustomAttributes(typeof(IndexAttribute), false);
NotMappedAttribute[] notMappedAttributes = (NotMappedAttribute[])property.GetCustomAttributes(typeof(NotMappedAttribute), false);
if (indexAttributes.Length > 0 && notMappedAttributes.Length == 0)
{
ColumnAttribute[] columnAttributes = (ColumnAttribute[])property.GetCustomAttributes(typeof(ColumnAttribute), false);
foreach (IndexAttribute indexAttribute in indexAttributes)
{
if (!indexes.ContainsKey(indexAttribute))
{
indexes.Add(indexAttribute, new List<string>());
}
if (property.PropertyType.IsValueType || property.PropertyType == typeof(string))
{
string columnName = columnAttributes.Length != 0 ? columnAttributes[0].Name : property.Name;
indexes[indexAttribute].Add(columnName);
}
else
{
indexes[indexAttribute].Add(property.PropertyType.Name + "_" + GetKeyName(property.PropertyType));
}
}
}
}
foreach (IndexAttribute indexAttribute in indexes.Keys)
{
query += CreateIndexQueryTemplate.Replace("{indexName}", indexAttribute.Name)
.Replace("{tableName}", tableName)
.Replace("{columnName}", string.Join(", ", indexes[indexAttribute].ToArray()))
.Replace("{unique}", indexAttribute.IsUnique ? "UNIQUE" : string.Empty);
}
}
if (context.Database.CreateIfNotExists())
{
context.Database.ExecuteSqlCommand(query);
}
}
private string GetKeyName(Type type)
{
PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo propertyInfo in propertyInfos)
{
if (propertyInfo.GetCustomAttribute(typeof(KeyAttribute), true) != null)
return propertyInfo.Name;
}
throw new Exception("No property was found with the attribute Key");
}
}
Then overload OnModelCreating in your dbcontext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new IndexInitializer<MyContext>());
base.OnModelCreating(modelBuilder);
}
Apply the index attribute to your Entity type, with this solution you can have multiple fields in the same index just use the same name and unique.
Extending Tsuushin's answer above to support multiple columns and unique constraints:
private void CreateIndex(RBPContext context, string field, string table, bool unique = false)
{
context.Database.ExecuteSqlCommand(String.Format("CREATE {0}NONCLUSTERED INDEX IX_{1}_{2} ON {1} ({3})",
unique ? "UNIQUE " : "",
table,
field.Replace(",","_"),
field));
}
expanding on Petoj
i modified the CreateIndexQueryTemplate to
private const string CreateIndexQueryTemplate = "IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = '{indexName}') CREATE {unique} INDEX {indexName} ON {tableName} ({columnName});";
and removed the following from OnModelCreating
Database.SetInitializer(new IndexInitializer<MyContext>());
and added the following to Configuration Seeding method
new IndexInitializer<MyContext>().InitializeDatabase(context);
this way the index attributes are run every time you do a update-database.
If you want this feature added to EF then you can vote for it here http://entityframework.codeplex.com/workitem/57
jwsadler's extension of Data Annotations was a nice fit for us. We use Annotations to influence the treatment of a class or property and Fluent API for global changes.
Our annotations cover indexes (unique and not unique) plus default values of getdate() and (1). The code sample shows how we applied it to our situation. All of our classes inherit from one base class. This implementation makes a lot of assumptions because we have a pretty simple model. We're using Entity Framework 6.0.1. Lots of comments have been included.
using System;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
namespace YourNameSpace
{
public enum SqlOption
{
Active = 1,
GetDate = 2,
Index = 3,
Unique = 4,
}
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public class SqlAttribute : Attribute
{
public SqlAttribute(SqlOption selectedOption = SqlOption.Index)
{
this.Option = selectedOption;
}
public SqlOption Option {get; set;}
}
// See enum above, usage examples: [Sql(SqlOption.Unique)] [Sql(SqlOption.Index)] [Sql(SqlOption.GetDate)]
public class SqlInitializer<T> : IDatabaseInitializer<T> where T : DbContext
{
// Create templates for the DDL we want generate
const string INDEX_TEMPLATE = "CREATE NONCLUSTERED INDEX IX_{columnName} ON [dbo].[{tableName}] ([{columnName}]);";
const string UNIQUE_TEMPLATE = "CREATE UNIQUE NONCLUSTERED INDEX UQ_{columnName} ON [dbo].[{tableName}] ([{columnName}]);";
const string GETDATE_TEMPLATE = "ALTER TABLE [dbo].[{tableName}] ADD DEFAULT (getdate()) FOR [{columnName}];";
const string ACTIVE_TEMPLATE = "ALTER TABLE [dbo].[{tableName}] ADD DEFAULT (1) FOR [{columnName}];";
// Called by Database.SetInitializer(new IndexInitializer< MyDBContext>()); in MyDBContext.cs
public void InitializeDatabase(T context)
{
// To be used for the SQL DDL that I generate
string sql = string.Empty;
// All of my classes are derived from my base class, Entity
var baseClass = typeof(Entity);
// Get a list of classes in my model derived from my base class
var modelClasses = AppDomain.CurrentDomain.GetAssemblies().ToList().
SelectMany(s => s.GetTypes()).Where(baseClass.IsAssignableFrom);
// For debugging only - examine the SQL DDL that Entity Framework is generating
// Manipulating this is discouraged.
var generatedDDSQL = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
// Define which Annotation Attribute we care about (this class!)
var annotationAttribute = typeof(SqlAttribute);
// Generate a list of concrete classes in my model derived from
// Entity class since we follow Table Per Concrete Class (TPC).
var concreteClasses = from modelClass in modelClasses
where !modelClass.IsAbstract
select modelClass;
// Iterate through my model's concrete classes (will be mapped to tables)
foreach (var concreteClass in concreteClasses)
{
// Calculate the table name - could get the table name from list of DbContext's properties
// to be more correct (but this is sufficient in my case)
var tableName = concreteClass.Name + "s";
// Get concrete class's properties that have this annotation
var propertiesWithAnnotations = concreteClass.GetProperties().Where(prop => Attribute.IsDefined(prop, annotationAttribute));
foreach (var annotatedProperty in propertiesWithAnnotations)
{
var columnName = annotatedProperty.Name;
var annotationProperties = annotatedProperty.GetCustomAttributes(annotationAttribute, true).ToList();
foreach (SqlAttribute annotationProperty in annotationProperties)
{
// Generate the appropriate SQL DLL based on the attribute selected
switch (annotationProperty.Option)
{
case SqlOption.Active: // Default value of true plus an index (for my case)
sql += ACTIVE_TEMPLATE.Replace("{tableName}", tableName).Replace("{columnName}", columnName);
sql += INDEX_TEMPLATE.Replace("{tableName}", tableName).Replace("{columnName}", columnName);
break;
case SqlOption.GetDate: // GetDate plus an index (for my case)
sql += GETDATE_TEMPLATE.Replace("{tableName}", tableName).Replace("{columnName}", columnName);
sql += INDEX_TEMPLATE.Replace("{tableName}", tableName).Replace("{columnName}", columnName);
break;
case SqlOption.Index: // Default for empty annotations for example [Sql()]
sql += INDEX_TEMPLATE.Replace("{tableName}", tableName).Replace("{columnName}", columnName);
break;
case SqlOption.Unique:
sql += UNIQUE_TEMPLATE.Replace("{tableName}", tableName).Replace("{columnName}", columnName);
break;
} // switch
} // foreach annotationProperty
} // foreach annotatedProperty
} // foreach concreteClass
// Would have been better not to go through all the work of generating the SQL
// if we weren't going to use it, but putting it here makes it easier to follow.
if (context.Database.CreateIfNotExists())
context.Database.ExecuteSqlCommand(sql);
} // InitializeDatabase
} // SqlInitializer
} // Namespace
Here's our context:
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace YourNameSpace
{
public class MyDBContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Only including my concrete classes here as we're following Table Per Concrete Class (TPC)
public virtual DbSet<Attendance> Attendances { get; set; }
public virtual DbSet<Course> Courses { get; set; }
public virtual DbSet<Location> Locations { get; set; }
public virtual DbSet<PaymentMethod> PaymentMethods { get; set; }
public virtual DbSet<Purchase> Purchases { get; set; }
public virtual DbSet<Student> Students { get; set; }
public virtual DbSet<Teacher> Teachers { get; set; }
// Process the SQL Annotations
Database.SetInitializer(new SqlInitializer<MyDBContext>());
base.OnModelCreating(modelBuilder);
// Change all datetime columns to datetime2
modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
// Turn off cascading deletes
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
}
To build further on all these great responses, we added the following code to enable the Index attribute to be picked up from an associated metadata type. For the full details please see my blog post, but in summary here are the details.
Metadata types are used like this:
[MetadataType(typeof(UserAccountAnnotations))]
public partial class UserAccount : IDomainEntity
{
[Key]
public int Id { get; set; } // Unique ID
sealed class UserAccountAnnotations
{
[Index("IX_UserName", unique: true)]
public string UserName { get; set; }
}
}
In this example the metadata type is a nested class, but it doesn't have to be, it can be any type. Property matching is done by name only, so the metadata type just has to have a property of the same name, and any data annotations applied to that should then be applied to the associated entity class. This didn't work in the original solution because it doesn't check for the associated metadata type. We plumbed in the following helper method:
/// <summary>
/// Gets the index attributes on the specified property and the same property on any associated metadata type.
/// </summary>
/// <param name="property">The property.</param>
/// <returns>IEnumerable{IndexAttribute}.</returns>
IEnumerable<IndexAttribute> GetIndexAttributes(PropertyInfo property)
{
Type entityType = property.DeclaringType;
var indexAttributes = (IndexAttribute[])property.GetCustomAttributes(typeof(IndexAttribute), false);
var metadataAttribute =
entityType.GetCustomAttribute(typeof(MetadataTypeAttribute)) as MetadataTypeAttribute;
if (metadataAttribute == null)
return indexAttributes; // No metadata type
Type associatedMetadataType = metadataAttribute.MetadataClassType;
PropertyInfo associatedProperty = associatedMetadataType.GetProperty(property.Name);
if (associatedProperty == null)
return indexAttributes; // No metadata on the property
var associatedIndexAttributes =
(IndexAttribute[])associatedProperty.GetCustomAttributes(typeof(IndexAttribute), false);
return indexAttributes.Union(associatedIndexAttributes);
}
For EF7 you can use the hasIndex() method.
We can set clustered and non-clustered index as well.
By default primary key will be clustered . We can change that behavior too.
supplierItemEntity.HasKey(supplierItem => supplierItem.SupplierItemId).ForSqlServerIsClustered(false);
supplierItemEntity.HasIndex(s => new { s.ItemId }).ForSqlServerIsClustered(true);
I discovered a problem with the answer #highace gave - the down migration uses the wrong override for DropIndex. Here is what I did:
To comply with Sql Server's limitation on index columns (900 bytes) I reduced the size of a couple of fields in my model
I added the migration using Add-Migration "Add Unique Indexes"
I manually added the CreateIndex and DropIndex methods to the migration. I used the override that takes the index name for the single column index. I used the override that takes an array of column names where the index spans more than one column
And here is the code with examples of both overrides of each method:
public partial class AddUniqueIndexes : DbMigration
{
public override void Up()
{
//Sql Server limits indexes to 900 bytes,
//so we need to ensure cumulative field sizes do not exceed this
//otherwise inserts and updates could be prevented
//http://www.sqlteam.com/article/included-columns-sql-server-2005
AlterColumn("dbo.Answers",
"Text",
c => c.String(nullable: false, maxLength: 400));
AlterColumn("dbo.ConstructionTypes",
"Name",
c => c.String(nullable: false, maxLength: 300));
//[IX_Text] is the name that Entity Framework would use by default
// even if it wasn't specified here
CreateIndex("dbo.Answers",
"Text",
unique: true,
name: "IX_Text");
//Default name is [IX_Name_OrganisationID]
CreateIndex("dbo.ConstructionTypes",
new string[] { "Name", "OrganisationID" },
unique: true);
}
public override void Down()
{
//Drop Indexes before altering fields
//(otherwise it will fail because of dependencies)
//Example of dropping an index based on its name
DropIndex("dbo.Answers", "IX_Text");
//Example of dropping an index based on the columns it targets
DropIndex("dbo.ConstructionTypes",
new string[] { "Name", "OrganisationID" });
AlterColumn("dbo.ConstructionTypes",
"Name",
c => c.String(nullable: false));
AlterColumn("dbo.Answers",
"Text",
c => c.String(nullable: false, maxLength: 500));
}
You can specify index in ModelBuilder
modelBuilder
.Entity<UserSalary>(builder =>
{
builder.HasNoKey();
builder.HasIndex("UserId").IsUnique(false);
builder.ToTable("UserSalary");
});
Since enumeration uses integers, what other structure can I use to give me enum-like access to the value linked to the name:
[I know this is wrong, looking for alternative]
private enum Project
{
Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1"),
Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2"),
Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1"),
Sales = new Guid("A5690E7-1111-4AFB-B44D-1DF3AD66D435"),
Replacement = new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"),
Modem = new Guid("6F686C73-504B-111-9A0B-850C26FDB25F"),
Audit = new Guid("30558C7-66D9-4189-9BD9-2B87D11190"),
Queries = new Guid("9985242-516A-4151-B7DD-851112F562")
}
EDIT 2014-07-20
This is a newer answer to this question. Using the Attribute class with a helper method, define the extra attributes needed on your enum.
public enum MultiValueEnum
{
[FooAttribute("alpha", 20d, true)]
First,
[FooAttribute("beta", 40.91d, false)]
Second,
[FooAttribute("gamma", 1.2d, false)]
Third,
}
public class FooAttribute : Attribute
{
internal FooAttribute(string name, double percentage, bool isGood)
{
this.Name = name;
this.Percentage = (decimal)percentage;
this.IsGood = isGood;
}
public string Name { get; private set; }
public decimal Percentage { get; private set; }
public bool IsGood { get; private set; }
}
public static TAttribute GetAttribute<TAttribute>(this Enum value)
where TAttribute : Attribute
{
var type = value.GetType();
var name = Enum.GetName(type, value);
return type.GetField(name)
.GetCustomAttributes(false)
.OfType<TAttribute>()
.SingleOrDefault();
}
Which makes it this easy:
MultiValueEnum enumVar = MultiValueEnum.First;
var enumStringValue = enumVar.GetAttribute<FooAttribute>().Name;
var enumValueDecimal = enumVar.GetAttribute<FooAttribute>().Percentage;
var enumBool = enumVar.GetAttribute<FooAttribute>().IsGood;
Otherwise you could create a custom Attribute for your enum, which can hold the Guid.
Something alongside these lines:
class EnumGuid : Attribute
{
public Guid Guid;
public EnumGuid(string guid)
{
Guid = new Guid(guid);
}
}
And you'd then use it like so:
enum Project
{
[EnumGuid("2ED3164-BB48-499B-86C4-A2B1114BF1")]
Cleanup = 1,
[EnumGuid("39D31D4-28EC-4832-827B-A11129EB2")]
Maintenance = 2
// and so forth, notice the integer value isn't supposed to be used,
// it's merely there because not assigning any value is a performance overhead.
}
And finally you could (I always do this) create an extension for easily getting the guid:
static Guid GetEnumGuid(this Enum e)
{
Type type = e.GetType();
MemberInfo[] memInfo = type.GetMember(e.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(EnumGuid),false);
if (attrs != null && attrs.Length > 0)
return ((EnumGuid)attrs[0]).Guid;
}
throw new ArgumentException("Enum " + e.ToString() + " has no EnumGuid defined!");
}
So in the end all you have to with your enums is:
Guid guid = Project.Cleanup.GetEnumGuid();
I use this approach to attach descriptions to enums, typically longer strings containing spaces, which thus cannot be used as names.
I've seen this method (struct) used by SubSonic to store Column and Table names.
internal struct Project
{
public static Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
public static Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A129EB2");
public static Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
public static Guid Sales = new Guid("A5690E7-1111-4AFB-B44D-1DF3AD66D435");
public static Guid Replacement = new Guid("11E5CBA2-EDDE-4ECA-BD63-B725C8C");
public static Guid Modem = new Guid("6F686C73-504B-111-9A0B-850C26FDB25F");
public static Guid Audit = new Guid("30558C7-66D9-4189-9BD9-2B87D11190");
public static Guid Queries = new Guid("9985242-516A-4151-B7DD-851112F562");
}
EDIT:- Thanks for commenting on deficiencies in code. In first place it will compile if the Guid strings are not invalid. As for not create instances to access variables yes they need to be public static
I would probably go the dictionary route on this one. Have a lookup table basically:
public class GuidMapper
{
private Dictionary<GuidTypes, Guid> mGuidMap = new Dictionary<GuidTypes, Guid>();
public enum GuidTypes: int
{
Cleanup,
Maintenance,
Upgrade,
Sales,
Replacement,
Modem,
Audit,
Queries
}
public GuidMapper()
{
mGuidMap.Add(GuidTypes.Cleanup, new Guid("2ED31640-BB48-499B-86C4-A2B1114BF100"));
mGuidMap.Add(GuidTypes.Maintenance, new Guid("39D31D40-28EC-4832-827B-A11129EB2000"));
mGuidMap.Add(GuidTypes.Upgrade, new Guid("892F8650-E38D-46D7-809A-49510111C100"));
mGuidMap.Add(GuidTypes.Sales, new Guid("A5690E70-1111-4AFB-B44D-1DF3AD66D435"));
mGuidMap.Add(GuidTypes.Replacement, new Guid("11E5CBA2-EDDE-4ECA-BDFD-63BDBA725C8C"));
mGuidMap.Add(GuidTypes.Modem, new Guid("6F686C73-504B-1110-9A0B-850C26FDB25F"));
mGuidMap.Add(GuidTypes.Audit, new Guid("30558C70-66D9-4189-9BD9-2B87D1119000"));
mGuidMap.Add(GuidTypes.Queries, new Guid("99852420-516A-4151-B7DD-851112F56200"));
}
public Guid GetGuid(GuidTypes guidType)
{
if (mGuidMap.ContainsKey(guidType))
{
return mGuidMap[guidType];
}
return Guid.Empty;
}
}
If you need proper enum-like semantics and type-safety then you can use a pattern like this.
(You could flesh it out further if you require extras like conversion operators, GetUnderlyingType, ToString etc. If you wanted to re-use the pattern for multiple enum-like classes with different underlying types then you could move any common code into a generic, abstract base class.)
Project x = Project.Cleanup;
Project y = Project.Cleanup;
Project z = Project.Maintenance;
Console.WriteLine(x == y); // True
Console.WriteLine(x == z); // False
Console.WriteLine(x.Value); // 47801daa-7437-4bfe-a240-9f7c583018a4
// this line will cause a compiler error
Console.WriteLine(x == new Guid("47801daa-7437-4bfe-a240-9f7c583018a4"));
// ...
public class Project
{
private Project(Guid v) { Value = v; }
public Guid Value { get; private set; }
public static readonly Project Cleanup =
new Project(new Guid("47801daa-7437-4bfe-a240-9f7c583018a4"));
public static readonly Project Maintenence =
new Project(new Guid("2548a7f3-7bf4-4533-a6c1-dcbcfcdc26a5"));
public static readonly Project Upgrade =
new Project(new Guid("ed3c3e73-8e6a-4c09-84ae-7f0876d194aa"));
}
When confronted with this kind of problem I used structs with consts as public members:
public struct FileExtensions
{
public const string ProcessingExtension = ".lck";
public const string ProcessedExtension = ".xml";
public const string FailedExtension = ".failed";
public const string CsvExtension = ".csv";
}
You could create a static class that just contains constant values.
For example:
internal static class Project
{
public static readonly Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
public static readonly Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
public static readonly Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
}
This way the class acts simply as a container and object cannot be created from it.
In VB this would be a Module:
Friend Module Project
Public Shared ReadOnly Cleanup As Guid = New Guid("2ED3164-BB48-499B-86C4-A2B1114BF1")
Public Shared ReadOnly Maintenance As Guid = New Guid("39D31D4-28EC-4832-827B-A11129EB2")
Public Shared ReadOnly Upgrade As Guid = New Guid("892F865-E38D-46D7-809A-49510111C1")
End Module
The enum type can only support the integral types (except char) as its value. You could however use something like a Dictionary to do lookups of a a name to a value.
Dictionary<Guid> lookup = new Dictionary<Guid>();
lookup["Cleanup"] = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
lookup["Maintenance"] = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
lookup["Upgrade"] = new Guid("892F865-E38D-46D7-809A-49510111C1");
// etc...
Another alternative is to have a series of readonly values in a static class.
public static class Guids
{
public static readonly Guid Cleanup = new Guid("2ED3164-BB48-499B-86C4-A2B1114BF1");
public static readonly Guid Maintenance = new Guid("39D31D4-28EC-4832-827B-A11129EB2");
public static readonly Guid Upgrade = new Guid("892F865-E38D-46D7-809A-49510111C1");
}