c# - EF Architecture, adding inherited entity to an existing table -
i´m not sure achieved, therefore ask guys!
say have existing table contains book register books written. can call table "books" (entity), , has bookid, booktitle , bookdescription. i´m using ef , database first approach, , working dbcontext.
very simple structure, know sell books , want take advantage of existing books table register users can browse on homepage. have used books sale want connect books table somehow.
in world structure base class named "book", , derived classes "newbook" , "usedbook", i`m totally lost how can achieve existing books table , entity framework.
i've had implement inheritance in ef in project , found difficult when creating subclasses tables in database first. instead, added subclasses entities in ef model, generated database script, , ran script add tables database. here steps took:
- in ef model diagram .edmx file right click background , add new... entity.
- type in entity name (in example usedbook) , select base type book.
- after clicking ok can add properties usedbook.
- right click background again , select generate database model. generate sql file.
- now if have data in existing database not want execute entire script drop existing tables. instead, find references of new table usedbook (will called books_usedbook in database) , execute lines. may need manually change reference book table may have pluralized incorrectly. make sure book table name in script matches what's in database.
- after executing script, go .edmx model, right click background, , select validate. shouldn't have errors here.
- in database have class books_usedbook bookid column referencing bookid column in books table.
hope helps!
Comments
Post a Comment