简体   繁体   中英

How do I map POCO model classes to existing DB tables using Entity Framework

We have existing POCO model classes and an existing DB. The OO model classes and relational DB tables don't map directly to each other.

How do I use Entity Framework to map these? All of the tutorials I have seen are either Code First (which goes and generates new DB tables), Model first (which generates new DB tables and POCOs) and DB First (which generates new POCOs).

I want none of these! I want to map between existing POCOs and DB tables. What's the best way of doing this? Can I use the visual designer, or do I have to do it in code?

Create mappings which map your entities to database structure (I suggest to use fluent mappings). Then just disable database generation:

Database.SetInitializer<YourContext>(null);

and provide connection string to existing database:

public class YourContext : DbContext
{
    public DatabaseContext(string connectionStringName)
      : base(connectionStringName)
    {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM