Howto specify table name with Entity Framework Code First Fluent API

c# code-first ef-code-first entity-framework

Question

I have an Entity and I am to configure Entity Framework to map it to a database table with different name.

I can easily do this with Code First DataAnnotations (DataAnnotations.Schema.TableAttribute).

But due to limitations now I have to use Code First Fluent API (my domain objects will be used by external clients, so they shouldn't be technology-specific - e.g. have any references to DataAnnotations)

I've searched on MSDN but found nothing. So is it possible and how?

Thank you.

1
79
12/22/2015 5:17:20 AM

Accepted Answer

You'll use the .ToTable() method:

modelBuilder.Entity<Department>().ToTable("t_Department");   

Source: MSDN: http://msdn.microsoft.com/en-us/data/jj591617.aspx

88
10/21/2014 3:14:30 PM

Popular Answer

You can also use the Table annotation:

[Table("InternalBlogs")]
public class Blog

See: Code First Data Annotations



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