0

I have view in SQL called ViewTest.

enter image description here

In code I have this model

    [Table("dbo.ViewTest")]
    public class ViewTest
    {
        public int Id { get; set; }

        public int EmployeeID { get; set; }
        public string PreferredName { get; set; }
        public string EmailPrimaryWork { get; set; }
        public string GeoCoverage { get; set; }
        public string Role { get; set; }
        public bool? LeftEmpFlag { get; set; }
    }

In configuration file:

public virtual IDbSet<ViewTest> ViewTests { get; set; }


protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new ViewConfiguration());
        }

 public class ViewConfiguration : EntityTypeConfiguration<ViewTest>
        {
            public ViewConfiguration()
            {
                this.HasKey(t => t.Id);
                this.ToTable("ViewTest");
            }
        }

I want to have connection like this one ViewTests.Where(....) ,but when I tried to do like this way I have error There is already an object named 'ViewTest' in the database..This means entity framework try to create new Table and I don`t want this.I want to access this view only!

Dany
  • 367
  • 1
  • 2
  • 21

1 Answers1

0

Well, it is code first so you should probably create your view in code. If that is not possible, then you need to tell EF not to create the table by commenting that line out of the Up() method on the migration. Once you update-database EF will have it in the metadata and you should be good to go.

Steve Greene
  • 12,029
  • 1
  • 33
  • 54