How to map exisiting sql server view with EF code first

后端 未结 2 1741
逝去的感伤
逝去的感伤 2021-01-02 23:19

i am fairly new in EF and learning EF code first. i am looking for a knowledge to map exisiting sql server view with EF code first. i have map my view with POCO but getting

2条回答
  •  孤街浪徒
    2021-01-02 23:55

    Configure view as table and use this custom generator to prevent migration generation for tables marked as views

    public class SkipViewGeneration : CSharpMigrationCodeGenerator
    {
        protected override void Generate(CreateTableOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(RenameTableOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(MoveTableOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(DropTableOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(AddColumnOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Table))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(DropColumnOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Table))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(DropPrimaryKeyOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Table))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(AlterColumnOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Table))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(AddPrimaryKeyOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Table))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(AlterTableOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(CreateIndexOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        protected override void Generate(DropIndexOperation operation, IndentedTextWriter writer)
        {
            if (!IsView(operation.Name))
                base.Generate(operation, writer);
        }
    
        private bool IsView(string tableNameWithSchemaName)
        {
            var tableName = DatabaseName.Parse(tableNameWithSchemaName).Name;
            var schemaName = DatabaseName.Parse(tableNameWithSchemaName).Schema;
            return schemaName.Contains("View");
        }
    }
    

    Usage example

    internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            CodeGenerator = new SkipViewGeneration();
        }
    
        protected override void Seed(AVMSDbContext context)
        {
            //  This method will be called after migrating to the latest version.
    
            //  You can use the DbSet.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.
        }
    }
    

    DatabaseName.Parse implementatnion you can get from Github sources

提交回复
热议问题