entity-framework – 使用Entity Framework创建数据库索引
发布时间:2020-12-12 08:25:22 所属栏目:MsSql教程 来源:网络整理
导读:说我有以下型号: [Table("Record")]public class RecordModel{ [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Display(Name = "Record Id")] public int RecordId { get; set; } [StringLength(150)] public string Name { get; s
说我有以下型号:
[Table("Record")] public class RecordModel { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] [Display(Name = "Record Id")] public int RecordId { get; set; } [StringLength(150)] public string Name { get; set; } [Required] [StringLength(15)] public string IMEI { get; set; } } 通过使用属性,数据注释或模型中的某些东西,可以向IMEI列添加索引吗? 解决方法根据这个链接: Creating Indexes via Data Annotations with Entity Framework 5.0你应该写一些类型的扩展代码: using System; [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; } } 和第二类: using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; using System.Linq; using System.Reflection; 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; 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); foreach (var 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 (var indexAttribute in indexAttributes) { string indexName = indexAttribute.Name; string tableName = tableAttributes.Length != 0 ? tableAttributes[0].Name : dataSetProperty.Name; string columnName = columnAttributes.Length != 0 ? columnAttributes[0].Name : property.Name; string query = CreateIndexQueryTemplate.Replace("{indexName}",indexName) .Replace("{tableName}",tableName) .Replace("{columnName}",columnName) .Replace("{unique}",indexAttribute.IsUnique ? "UNIQUE" : string.Empty); context.Database.CreateIfNotExists(); context.Database.ExecuteSqlCommand(query); } } } } } } 之后,您可以使用您的索引: [Required] [Index("IMEIIndex",unique: true)] [StringLength(15)] public string IMEI { get; set; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- connection string大全1
- 如何使用dbUnit将数据库恢复到初始状态?
- sql server几种Join的区别测试方法
- Android判断NavigationBar是否显示的方法(获取屏幕真实的高
- sql-server – GRANT用户查看单个数据库? (拒绝查看其余部
- sql-server – SQL Server:sys.master_files与sys.databas
- SQLServer分页查询通用存储过程
- SQLserver删除某数据库中所有表实现思路
- sqlserver2005 递归查询
- Sqlserver中tinyint, smallint, int, bigint的区别 及 10进