Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind my MVC Model to a different table

This seems like an easy question but I can't seem to find the answer.

I have a Model that looks like this ...

public class Application
{

    public int Id { get; set; }
    public string Title { get; set; }
    public string LeadProgrammer { get; set; }
    public string ConnectionStringCode { get; set; }
}

public class ApplicationDBContext : DbContext
{
    public DbSet<Application> Applications { get; set; }
}

My actual table name is DBA_APPLICATIONS ... the model is, of course, just looking for dbo.Applications. How can I change this routing to the actual table?

like image 869
cavillac Avatar asked Nov 24 '25 14:11

cavillac


1 Answers

Add this in your ApplcationDBContext class.

public class ApplicationDBContext : DbContext
{
    public DbSet<Application> Applications { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Application>().ToTable("DBA_APPLICATIONS");
        // otherwise EF assumes the table is called "Applications"
    }
}
like image 176
Tomislav Markovski Avatar answered Nov 26 '25 05:11

Tomislav Markovski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!