5

We are making an EntityFramework CodeFirst DAL over a legacy database (which means that we are mostly stuck with whatever design errors it has).

Domain model is (pretty) simple: we have abstract Card, subtyped into HomeCard, CarCard, OwnerCard and RenterCard. To be precise:

abstract class Card {}
class HomeCard: Card {}
class CarCard: Card {}
class OwnerCard: Card {}
class RenterCard: Card {}

Database model follows it with tables Cards, Homes, Cars, Owners and Renters, where Cards contains common columns, and other tables contain columns pertaining to specific class. Which is exactly what TPT means, and it maps perfectly to EntityFramework.

After refactoring we've found that (not unexpectedly) Home and Car do share some properties, and Owner and Renter do the same. And it is not only for the sake of beauty, but some functionality would be much easier to implement (e.g. searching Owners and Renters by name, or Homes and Cards by owner). So we expect our domain model to look like this:

abstract class Card {}
abstract class ProperyCard: Card {}
class HomeCard: ProperyCard {}
class CarCard: ProperyCard {}
abstract class PersonCard: Card {}
class OwnerCard: PersonCard {}
class RenterCard: PersonCard {}

But we still have the same db model, which means that we have some weird mix between TPT and TPC. All our attempts to map it via EF/CodeFirst have failed. Any advice on making it work?

PS Our base table, Cards, does have a discriminator field, if that could be of any help.

Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35

1 Answers1

0

You have an existing database therefore you DO NOT want to use code first. Add an entity data model item to your project and hook it up to your existing database with the provided wizard.

I do not recommend the following but if you MUST make your CF model work with an existing database try the following steps.

  1. Have CF generate a new database.
  2. Copy the EDMMetaData (<EF4.3.1) or __MigrationHistory (>=EF.4.3.1) table to your existing database. This will trick EF into thinking it created the DB and that it matches.

Note: You will most likely get errors down the line when working with the CF framework against your legacy database. If your CF framework is not perfect, it will suffer.

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
  • I *want* to use CodeFirst because of entirely different mapping model. It always seemed to me that the name is wrong, it should be Code*Only*, not Code*First*. – Sergei Rogovtcev Jul 24 '12 at 11:45
  • @SergRogovtsev See edits. I've added a suggestion on how to *trick* your CF framework into using your legacy database. – Paul Fleming Aug 15 '12 at 10:20
  • You haven't understand my problem. I have no trouble with making EF/CF think it matches database. My problem is that we've found no working mapping configuration that supports our particular inheritance scenario. – Sergei Rogovtcev Aug 15 '12 at 12:14
  • I'm using an existing DB and Code First has worked extremely well for me. I had all kinds of problems using the EF designer due to bugs & the complexity of the EDM configuration. All you have to do is essentially delete everything in your initial migration, and make sure the initial data model maps perfectly onto the database. Once you do that, your model is effectively a Code First model. – HiredMind Oct 29 '13 at 18:00