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

asp.net-mvc asp.net-mvc-4 c# entity-framework entity-framework-6

Question

I have a scenario where I create a table from a SQL Server database as an Entity Framework model, but it is connected to 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 issue is that I want to dynamically take data inside an MVC controller that is called by Ajax. I must therefore provide the second table's name in Ajax parameters. Is it possible to alter the name ofTable2 using MetadataType property by adding a different classTable1Metadata ?

1
-1
4/30/2016 10:58:41 AM

Accepted Answer

The fluent api can be used to modify a table's name:

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

You can override this procedure in yourDbContext .

0
4/30/2016 9:55:37 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow