بازنویسی سادهتر پیش فرضهای EF Code first در نگارش 6 آن
نویسنده: وحید نصیری
تاریخ: ۱۳۹۲/۱۱/۰۶ ۱۴:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// TableNameConvention
modelBuilder.Types()
.Configure(entity => entity.ToTable("tbl" + entity.ClrType.Name));
base.OnModelCreating(modelBuilder);
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// PrimaryKeyNameConvention
modelBuilder.Properties()
.Where(p => p.Name == "Id")
.Configure(p => p.IsKey().HasColumnName(p.ClrPropertyInfo.ReflectedType.Name + "Id"));
base.OnModelCreating(modelBuilder);
} public class StringConventions : IStoreModelConvention<EdmProperty>
{
public void Apply(EdmProperty property, DbModel model)
{
if (property.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.String)
{
property.MaxLength = 450;
}
}
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add<StringConventions>();
base.OnModelCreating(modelBuilder);
} public class MyConventions : Convention
{
public MyConventions()
{
// PrimaryKeyNameConvention
this.Properties()
.Where(p => p.Name == "Id")
.Configure(p => p.IsKey().HasColumnName(p.ClrPropertyInfo.ReflectedType.Name + "Id"));
// TableNameConvention
this.Types()
.Configure(entity => entity.ToTable("tbl" + entity.ClrType.Name));
}
} public class CustomMaxLengthConvention : Convention
{
public CustomMaxLengthConvention()
{
this.Properties<string>().Configure(p => p.HasMaxLength(450));
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.AddAfter<MaxLengthAttributeConvention>(new CustomMaxLengthConvention()); //برای یک خاصیت مشخص در یک کلاس مشخص
modelBuilder.Entity<Department>().Ignore(t => t.Budget);
// برای یک کلاس مشخص
modelBuilder.Ignore<OnlineCourse>();
// برای نام خاصیتی مشخص در تمام کلاسهای نگاشت شده
modelBuilder.Types().Configure(c => c.Ignore("IsDeleted"));
// صرفنظر کردن از تمام ایینامها در تمام کلاسهای نگاشت شده
modelBuilder.Types().Configure(typeConfiguration =>
{
foreach (var property in typeConfiguration.ClrType
.GetProperties().Where(p => p.PropertyType.IsEnum))
{
typeConfiguration.Ignore(property);
}
});
// صرفنظر کردن از خواصی که با یک نام مشخص شروع میشوند در تمام کلاسها
modelBuilder.Types().Configure(typeConfiguration =>
{
foreach (var property in typeConfiguration.ClrType
.GetProperties().Where(p => p.Name.StartsWith("someName")))
{
typeConfiguration.Ignore(property);
}
}); modelBuilder.Types().Configure(c=>c.Ignore("IsDeleted")); You cannot use Ignore method on the property 'IsDeleted' on type 'MyEntity' because this type inherits from the type 'BaseEntity' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type.
modelBuilder.Entity<BaseEntity>().Ignore(p => p.IsDeleted);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();