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 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 ?

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

Accepted Answer

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.

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