Beispiel #1
0
        public static DbContext DbContext()
        {
            var typeMappingSource = new SqlServerTypeMappingSource(new TypeMappingSourceDependencies(new ValueConverterSelector(new ValueConverterSelectorDependencies())), new RelationalTypeMappingSourceDependencies()); // EF Core 2.1

            // From SQL type to C# type.
            var type = typeMappingSource.FindMapping("nvarchar(max)").ClrType;

            var conventionSet = SqlServerConventionSetBuilder.Build();
            var builder       = new ModelBuilder(conventionSet);

            // My
            var entity = builder.Entity(typeof(My));

            entity.ToTable("My");
            entity.Property("Id");
            entity.Property("Id").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(int)));
            entity.Property("Text").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(string)));

            // MyView
            entity = builder.Entity(typeof(MyView));
            entity.ToTable("MyView");
            entity.Property("Id");
            entity.Property("Id").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(int)));
            entity.Property("Text").HasAnnotation(CoreAnnotationNames.TypeMapping, typeMappingSource.FindMapping(typeof(string)));

            var model   = builder.Model;
            var options = new DbContextOptionsBuilder();

            options.UseSqlServer("Data Source=localhost; Initial Catalog=AdventureWorks2016; Integrated Security=True;").UseModel(model);

            var dbContext = new DbContext(options.Options);

            return(dbContext);
        }
Beispiel #2
0
        private static IMutableModel DbContextModel(Type typeRow)
        {
            // EF Core 2.1
            var typeMappingSource = new SqlServerTypeMappingSource(new TypeMappingSourceDependencies(new ValueConverterSelector(new ValueConverterSelectorDependencies())), new RelationalTypeMappingSourceDependencies());

            var conventionSet = SqlServerConventionSetBuilder.Build();
            var builder       = new ModelBuilder(conventionSet);

            // Build model
            var entity = builder.Entity(typeRow);
            SqlTableAttribute tableAttribute = (SqlTableAttribute)typeRow.GetTypeInfo().GetCustomAttribute(typeof(SqlTableAttribute));

            entity.ToTable(tableAttribute.SqlTableName, tableAttribute.SqlSchemaName); // By default EF maps sql table name to class name.
            PropertyInfo[] propertyInfoList = TypeRowToPropertyList(typeRow);
            bool           isPrimaryKey     = false;                                   // Sql view

            foreach (PropertyInfo propertyInfo in propertyInfoList)
            {
                SqlFieldAttribute columnAttribute = (SqlFieldAttribute)propertyInfo.GetCustomAttribute(typeof(SqlFieldAttribute));
                if (columnAttribute == null || columnAttribute.SqlFieldName == null) // Calculated column. Do not include it in sql select.
                {
                    entity.Ignore(propertyInfo.Name);
                }
                else
                {
                    if (columnAttribute.IsPrimaryKey)
                    {
                        isPrimaryKey = true;
                        entity.HasKey(propertyInfo.Name); // Prevent null exception if primary key name is not "Id".
                    }
                    entity.Property(propertyInfo.PropertyType, propertyInfo.Name).HasColumnName(columnAttribute.SqlFieldName);
                    CoreTypeMapping coreTypeMapping = typeMappingSource.FindMapping(propertyInfo.PropertyType);
                    Debug.Assert(coreTypeMapping != null);
                    entity.Property(propertyInfo.PropertyType, propertyInfo.Name).HasAnnotation(CoreAnnotationNames.TypeMapping, coreTypeMapping);
                }
            }

            if (isPrimaryKey == false)
            {
                entity.HasKey(propertyInfoList.First().Name); // Prevent null exception if name of first field (of view) is not "Id".
            }

            var model = builder.Model;

            return(model);
        }