none
How to map existing sql server view with EF code first RRS feed

  • General discussion

  • 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 the below error.

    when i try to fetch data from view then got the below error thrown

    Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database

    my full code as follow

    public class TestDBContext : DbContext
        {
            public TestDBContext()
                : base("name=TestDBContext")
            {
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new vwCustomerConfiguration());
            }
    
            public DbSet<vwCustomer> vwCustomer { get; set; }
        }
    
    public class vwCustomerConfiguration : EntityTypeConfiguration<vwCustomer>
    {
        public vwCustomerConfiguration()
        {
            this.HasKey(t => t.CustomerID);
            this.ToTable("vwCustomer");
        }
    }
    
          public class vwCustomer
            {
                public int CustomerID { get; set; }
                public string FirstName { get; set; }
    
            }

    this way i am trying to load data.

        using (var db = new TestDBContext())
        {
            var listMyViews = db.vwCustomer.ToList();
        }

    guide me what i am missing in code for which error is throwing. thanks

    • Edited by Sudip_inn Friday, September 9, 2016 1:54 PM
    Friday, September 9, 2016 12:00 PM