How to set a database column as sparse when using the EF Code First Fluent API?

I work with a system that uses EF code first, and I would like to use multiple sparse SQL Server columns for a table. My current solution is to delete the table created by EF and re-add via script during database initialization. Is this something that can be configured with the Fluent API in a class inherited from EntityTypeConfiguration or in other ways?

+6
source share
1 answer

If you use Entity Frameworks migrations, you can issue an SQL query like this in the Up method for migration, which adds a sparse column:

Sql("alter table TableName alter column ColumnName int sparse"); 

Even if you do not use migrations, any single execution of dbContext.Database.ExecuteSqlCommand with the same SQL will work.

None of the methods are as good as if you could explicitly configure the type through EF, but they are still better than deleting and replacing the entire table.

+3
source

Source: https://habr.com/ru/post/913034/


All Articles