Aliases for complex type property names in the Entity framework

My database administrator calls its column names something else than how our code uses table data (e.g. description and product name). I am wondering if I have a complex type in the Entity Framework, is it possible to give a property an alias?

+4
source share
3 answers

You can name the property as you want, and simply map the database field to this property. The name of the properties is independent of the names of the corresponding database columns.

+5
source

Are you using EntityFramework Code-First? When you mention a complex type, this is what I think of. If so, it will help you get a better answer if you provide this information ...

If so, here is a great ScottGu article on Customizing a User Database Schema

The code looks something like this:

protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<YourEntityName>().MapSingleType(t => new { columnId = t.Id, description = t.ProductName // If database column name is description and your entity property name is ProductName product_name = t.Description // If it the other way around... }).ToTable("DatabaseTableName"); } 
+3
source

Visual Studio has information about displaying a screen / panel. By default, it is usually visible on the lower tabs next to the error list, exit, etc. When "EF4" opens on your designer screen. In the designer view, when you click on a table, the panel below fills in the columns (from the database table) and the values โ€‹โ€‹/ properties (from your class). In values โ€‹โ€‹/ properties, you can change the name and type to whatever you want for your class. I do this regularly when the database administrator begins to create garbage column names or starts using prefixes in some places and not in others.

Hope this helps.

EDIT:

Here is an example of how to change a complex type, if it is really necessary, http://blogs.msdn.com/b/wriju/archive/2010/07/26/ef4-complex-type.aspx

0
source

All Articles