C# 使用数据注释的唯一索引
是否有一种使用数据注释定义唯一索引的方法 假设我有一门课:C# 使用数据注释的唯一索引,c#,entity-framework,ef-code-first,indexing,data-annotations,C#,Entity Framework,Ef Code First,Indexing,Data Annotations,是否有一种使用数据注释定义唯一索引的方法 假设我有一门课: public class User { [Key] public int UserID { get; set; } public string UserName { get; set; } public string Email { get; set; } } 如果我希望电子邮件是唯一的,我该如何向其添加唯一的索引?这与我已经回答的问题几乎相同 根据这个链接: 您应该编写某种扩展代码: using Syste
public class User
{
[Key]
public int UserID { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
}
如果我希望电子邮件是唯一的,我该如何向其添加唯一的索引?这与我已经回答的问题几乎相同 根据这个链接: 您应该编写某种扩展代码:
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);
}
}
}
}
}
}
你应该将这个问题标记为重复,而不是重复回答。@GertArnold-done-MarkedTanks。但我认为你也应该删除这个答案。冗余内容是一个问题,因为内容可以随时编辑(=改进)。如果你对该代表感兴趣(谁不感兴趣?),那么觉得这个答案有帮助的人可能会在重定向到另一个答案时,将其换成另一个答案。还有各种各样闪亮的徽章可以为高投票率的答案赢得嗯,只是一个评论。两个好的答案仍然比一个差的答案要好:)@GertArnold——我不确定它是否100%是同一个问题——这就是为什么我重新发布了我的答案answer@GertArnold-我也不知道为什么版主不把它标记为复制品。我已经标记过了。也许你也可以把它标记为dupplicate——然后版主会做出反应=)
[Required]
[Index("IMEIIndex", unique: true)]
[StringLength(15)]
public string IMEI { get; set; }