0

I have a model with several entities in my MVC4 project with VS 2012. Recently I added a view to my DB named 'vwTeacherNames' and I tried to update the model and I unchecked the Plorizing option for that update.

Then, I rename my entity to 'TeacherName'. Now when I tun the Prj, this exception is thrown where I define a DropDownList for teachers:

Invalid object name 'dbo.TeacherNames'.

I tried many ways such as using custom tool, removing the .tt files and generating the again, ... However the problem stays firm!

So, how can I tell the EF the right table(in fact view) name which is vwTeacherNames?

Thanks a lot

1 Answer 1

1

Found it! and I add it here with some more tweaks:

public class myDbContext : DbContext
{
    public PtDbContext()
        : base("DefaultConnection")
    {

    }

    ... //some entities

    //Here it is:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TeacherName>().Property(t => t.FullName)
            .HasColumnName("TeacherName");

        modelBuilder.Entity<TeacherName>().ToTable("vwTeacherNames", schemaName: "dbo");                        

    }
}

Update: Why waisting your time by defining what you previously defined?! Just kill the default table naming convention and enjoy progressing your Prj:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Adding this line tells the EF not to go through that convention
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();               
    }

So, It should builds up your queries by EntitySetName and EntityName properties of your entities which the first of is the DB table name and the second is your entity name which you use in your DbContext.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.