Rey
Rey

Reputation: 4002

Rename a property or give an alias name in Entity Framework?

I have a situation where I generate a table from a SQL Server database as a model with Entity Framework, but it is related with another table in the database so inside the model is also a property with the second table name like this

public virtual ICollection<Table2> Table2 { get; set; }

The problem is that I what to take data dynamically inside a controller in MVC which is called by Ajax. And as a result I have to expose the name of the second table in Ajax arguments. Is there any way to change the name of Table2 using MetadataType attribute by creating another class Table1Metadata ?

Upvotes: 1

Views: 3072

Answers (1)

SynerCoder
SynerCoder

Reputation: 12766

You can use the fluent api to change a table name:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Table2>().ToTable("MyCustomTable2Name");
}

This method can be overriden in your DbContext.

Upvotes: 1

Related Questions