Beispiel #1
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            if (Database.IsSqlServer())
            {
            }

            modelBuilder.ApplyConfiguration(new BlogConfig());
            modelBuilder.ApplyConfiguration(new BLogWriterConfig());
            modelBuilder.Ignore <Person>();
            base.OnModelCreating(modelBuilder);
        }
Beispiel #2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            #region Trick for pseudo entities for grouping and Views

            var p = System.Diagnostics.Process.GetCurrentProcess();
            //Console.WriteLine(p.ProcessName + "/" + System.Diagnostics.Debugger.IsAttached);


            if (!IsRuntime)
            {
                builder.Ignore <UserStatistics>();
            }


            #endregion

            // In this case, EFCore can derive the database schema from the entity classes by convention and annotation.
            // The following Fluent API configurations only change the default behavior!
            #region Shadow property
            if (AdditionalColumnSet != null)
            {
                foreach (string shadowProp in AdditionalColumnSet)
                {
                    var    splitted    = shadowProp.Split(';');
                    string entityclass = splitted[0];
                    string columnname  = splitted[1];
                    string columntype  = splitted[2];

                    Type columntypeObj = Type.GetType(columntype);

                    builder.Entity(entityclass).Property(columntypeObj, columnname);
                }
            }
            #endregion

            #region Mass configuration via model class (--> Custom Convention)
            foreach (IMutableEntityType entity in builder.Model.GetEntityTypes())
            {
                // EFC Standard is to use the DbSet property name as table name
                // e.g. class "Task" --> table "TaskSet"
                // this code will change this convention
                // all table names = class names (as with EF 6.x),
                // except the classes that have [Table] annotation
                var annotation = entity.ClrType.GetCustomAttribute <TableAttribute>();
                if (annotation == null)
                {
                    entity.Relational().TableName = entity.DisplayName();
                }
            }
            #endregion

            #region Computed Column
            if (Database.IsSqlServer())
            {
                builder.Entity <Task>().Property(x => x.DueInDays)
                .HasComputedColumnSql("DATEDIFF(day, GETDATE(), [Due])");
            }

            builder.Entity <Task>().Property(x => x.Title).HasDefaultValue(BO.Task.DefaultTitle);
            #endregion

            #region Custom Indices
            builder.Entity <Category>().HasIndex(x => x.Name);
            builder.Entity <Task>().HasIndex(x => x.Title);
            builder.Entity <Task>().HasIndex(x => x.Done);
            builder.Entity <Task>().HasIndex(x => x.Due);
            builder.Entity <Task>().HasIndex(x => new { x.Title, x.Due });
            #endregion
        }