Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 919
Anthony Avatar asked Apr 18 '26 10:04

Anthony


1 Answers

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)
    {
    }
}
like image 105
Sergey Berezovskiy Avatar answered Apr 19 '26 23:04

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!