DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Entity Framework 5 First Steps – Mapping

Entity Framework 5 First Steps – Mapping

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Sep. 30, 12 · Interview
Like (0)
Save
Tweet
Share
19.43K Views

Join the DZone community and get the full member experience.

Join For Free

in a previous article , i’ve explained how simple it is to use ef5 to access a database and use database-migrations to create a database suitable to contain your entities (with a simple update-command from the package manager console). when you show this technique to many programmers, the first complaint they have is, “ the structure of the generated tables is not suitable to me .”

image

figure 1: database generated with previous code example

suppose you want to specify a maximum length for the various columns, specify not-null columns, and you want them to be named with a convention. finally, you don't want to use identity columns, because all the customers you will save in this database come from a legacy cmr application and you want to be able to use the very same id of the crm system.

if you want to customize how the table is generated, but you want to be completely free on how you name entities, properties, or context dbset, you need to specify a custom mapping that does not use standard convention over configuration. first you must create a mapping class for your customer class (you can also do this with attributes) to specify how properties will be mapped on database tables and columns .

public class customermap : entitytypeconfiguration<customer>
{
    public customermap()
    {
        // primary key
        this.haskey(t => t.id);
 
        // properties
        // table & column mappings
        this.totable("customer");
        this.property(t => t.id)
            .hascolumnname("cust_id")
            .hasdatabasegeneratedoption(databasegeneratedoption.none);
        this.property(t => t.name)
            .hascolumnname("cust_name")
            .hasmaxlength(100)
            .isrequired();
        this.property(t => t.address)
            .hascolumnname("cust_address")
            .hasmaxlength(200);
    }
}

you can put this class wherever you want, i usually put it into a subfolder named mappings and i usually call this class with the same name of the entity with the suffix map. now you should only specify in your dbcontext that this mapping is available .

protected override void onmodelcreating(dbmodelbuilder modelbuilder)
{
    modelbuilder.configurations.add(new customermap());
}

by overriding onmodelcreating you can add your custom mapping to the configurations collection , now if you issue another update-database you can verify that the table is now created the way you want.

image

figure 2: new structure of the table that respect the customermap class

now you can see that the table is called customer, columns are named with your convention and length and not null are created correctly. this is really good because if the dba or some convention impose you some naming rules on database tables and column you are free to use business-friendly names in your entities .

Database Entity Framework Framework

Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Enabling DB Migrations Using Kubernetes Init
  • The Importance of Delegation in Management Teams
  • How and Why You Should Start Automating DevOps
  • Why Open Source Is Much More Than Just a Free Tier

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: