本文整理汇总了C#中DbModelBuilder.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# DbModelBuilder.GetType方法的具体用法?C# DbModelBuilder.GetType怎么用?C# DbModelBuilder.GetType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbModelBuilder
的用法示例。
在下文中一共展示了DbModelBuilder.GetType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureEntities
private static void ConfigureEntities(Type dbContextType, DbModelBuilder modelBuilder)
{
Contract.Requires<ArgumentNullException>(dbContextType != null, "dbContextType");
Contract.Requires<ArgumentNullException>(modelBuilder != null, "modelBuilder");
Contract.Assume(modelBuilder.Configurations != null, "EF Should always populate the Configurations property of the modelBuilder");
// get all configurations
IEnumerable<Type> configuredEntityTypes = GetConfigurations(dbContextType);
// get all entities
IEnumerable<Type> allEntityTypes = GetEntities(dbContextType);
// determine unconfigured types
IEnumerable<Type> unconfiguredEntityTypes =
allEntityTypes.Except(configuredEntityTypes);
// Let EF configure itself using the configurations
modelBuilder.Configurations.AddFromAssembly(dbContextType.Assembly);
// And do the remaining ourselfs
MethodInfo genericMethod =
modelBuilder.GetType().GetMethods()
.Single(e => e.Name == "Entity" && e.IsGenericMethodDefinition);
foreach (Type unconfiguredEntity in unconfiguredEntityTypes)
{
MethodInfo method = genericMethod.MakeGenericMethod(new[] { unconfiguredEntity });
method.Invoke(modelBuilder, new object[0]);
}
}
示例2: ConfigureEntities
/// <summary>
/// Automatic context's registration for all of domain entities,
/// disable it for scenario when store model are different from your domain,
/// and add models to the context manually.
/// You can add different models then the domain ones to DB if needed
/// </summary>
/// <param name="builder">Model builder</param>
private static void ConfigureEntities(DbModelBuilder builder)
{
var baseEntityType = typeof(BaseEntity);
var domainAssembly = Assembly.GetAssembly(baseEntityType);
var entitiesTypes = domainAssembly.ExportedTypes
.Where(t => t.IsClass && t != baseEntityType && baseEntityType.IsAssignableFrom(t));
foreach (var type in entitiesTypes)
{
var entityMethod = builder.GetType().GetMethod("Entity").MakeGenericMethod(new Type[] { type });
entityMethod.Invoke(builder, null);
}
}
示例3: OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//modelBuilder.Entity<SecondLevelObjectBase>().ToTable("t_SecondLevelObjectBase", "Core");
//modelBuilder.Entity<EFLab.DAL.BizObjects.TypeA.TypeASecondLevelObject>().ToTable("t_TypeASecondLevel", "TypeA");
//modelBuilder.Entity<EFLab.DAL.BizObjects.TypeB.TypeBSecondLevelObject>().ToTable("t_TypeBSecondLevel", "TypeB");
//modelBuilder.Entity<EFLab.DAL.BizObjects.TypeC.TypeCSecondLevelObject>().ToTable("t_TypeCSecondLevel", "TypeC");
//modelBuilder.Entity<SecondLevelObjectBase>()
// .Property(c => c.SecondLevelObjectBaseId)
// .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
//modelBuilder.Entity<TypeASecondLevelObject>().Map(m =>
//{
// m.MapInheritedProperties();
// m.ToTable("t_SecondLevelObjectA", "TypeA");
//});
//modelBuilder.Entity<TypeBSecondLevelObject>().Map(m =>
//{
// m.MapInheritedProperties();
// m.ToTable("t_SecondLevelObjectB", "TypeB");
//});
/*
System.Data.Entity.DbModelBuilder
public virtual EntityTypeConfiguration<TEntityType> DbModelBuilder.Entity<TEntityType>()
where TEntityType : class
Registers an entity type as part of the model and returns an object that can be used to configure the entity.
This method can be called multiple times for the same entity to perform multiple lines of configuration.
https://aleemkhan.wordpress.com/2013/02/28/dynamically-adding-dbset-properties-in-dbcontext-for-entity-framework-code-first/
http://stackoverflow.com/questions/14843462/how-can-i-use-reflection-to-invoke-dbmodelbuilder-entityt-ignorex-x-proper
http://stackoverflow.com/questions/15481685/querying-against-dbcontext-settypevariable-in-entity-framework
http://stackoverflow.com/questions/19644617/linq-multiple-join-iqueryable-modify-result-selector-expression
*/
MethodInfo method = modelBuilder.GetType().GetMethod("Entity");
method = method.MakeGenericMethod(new Type[] { typeof(EFLab.DAL.BizObjects.TypeA.TypeAObject1) });
method.Invoke(modelBuilder, null);
//this.AddDbSet(modelBuilder, typeof(EFLab.DAL.BizObjects.TypeA.TypeAObject1));
//this.AddDbSet(modelBuilder, typeof(EFLab.DAL.BizObjects.TypeB.TypeBObject1));
//this.AddDbSet(modelBuilder, typeof(EFLab.DAL.BizObjects.TypeC.TypeCObject1));
//EntityTypeConfiguration<TopLevelObject> config = modelBuilder.Entity<TopLevelObject>();
base.OnModelCreating(modelBuilder);
}
示例4: OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (var map in configuration.Configurations)
{
modelBuilder.Configurations.Add((dynamic)map);
}
foreach (var dbset in configuration.DbSets)
{
MethodInfo method = modelBuilder.GetType().GetMethod("Entity");
method = method.MakeGenericMethod(new Type[] { dbset });
method.Invoke(modelBuilder, null);
}
base.OnModelCreating(modelBuilder);
}
示例5: OnModelCreating
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
foreach (var entityAssembly in _entityAssemblies)
{
var entityTypes = entityAssembly.GetTypes()
.Where(type => (!type.IsAbstract && IsAssignableToGenericType(type, typeof(IEntity<>))))
.ToList();
foreach (var entityType in entityTypes)
{
var method = modelBuilder.GetType().GetMethod("Entity");
method = method.MakeGenericMethod(new[] { entityType });
method.Invoke(modelBuilder, null);
}
}
}
示例6: AddDbSet
private void AddDbSet(DbModelBuilder modelBuilder, Type type)
{
MethodInfo method = modelBuilder.GetType().GetMethod("Entity");
method = method.MakeGenericMethod(new Type[] { type });
method.Invoke(modelBuilder, null);
}
示例7: HandleDecimalPrecision
private void HandleDecimalPrecision(DbModelBuilder modelBuilder)
{
IEnumerable<Type> types = GetDbContextApplicableTypes();
foreach (Type classType in types)
{
foreach (
var propAttr in
classType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.GetCustomAttribute<DecimalPrecisionAttribute>() != null)
.Select(
p => new { prop = p, attr = p.GetCustomAttribute<DecimalPrecisionAttribute>(true) }))
{
object entityConfig =
modelBuilder.GetType()
.GetMethod("Entity")
.MakeGenericMethod(classType)
.Invoke(modelBuilder, null);
ParameterExpression param = Expression.Parameter(classType, "c");
Expression property = Expression.Property(param, propAttr.prop.Name);
LambdaExpression lambdaExpression = Expression.Lambda(property, true,
new[] { param });
DecimalPropertyConfiguration decimalConfig;
if (propAttr.prop.PropertyType.IsGenericType &&
propAttr.prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
MethodInfo methodInfo =
entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[7];
decimalConfig =
(DecimalPropertyConfiguration)methodInfo.Invoke(entityConfig, new object[] { lambdaExpression });
}
else
{
MethodInfo methodInfo =
entityConfig.GetType().GetMethods().Where(p => p.Name == "Property").ToList()[6];
decimalConfig =
(DecimalPropertyConfiguration)methodInfo.Invoke(entityConfig, new object[] { lambdaExpression });
}
decimalConfig.HasPrecision(propAttr.attr.Precision, propAttr.attr.Scale);
}
}
}