简体   繁体   中英

C# Entity Framework: create and save (abstract class)

I have an existing project where I need to create some new instances of a class and save them to a database. The project uses Entity Framework, which I am not really familiar with.

I need to grab a set of items from a database that is of type CLASS A (abstract class). Since I cannot create instances of abstract classes, I created CLASS B that inherits from CLASS A.

But, when I try to pull the CLASS A records from DB and return them as CLASS B items, the system throws an error

The entity type CLASS B is not part of the model for the current context

So, I assume CLASS B must be mapped against CLASS A somehow? But I'm not sure how?

For mapping objects, you can use AutoMapper (at least that is what I always use for mapping objects from A <-> B). It's pretty easy. https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

Further, I'm not sure, but I don't think you can't use inheritance when it comes to Entity framework classes. You should just make 2 seperate classes. (So probably a classB with the same fields as classA, but you can also add/remove fields as much as you want and fill them up in your mapping).

I hope the answer is any helpfull

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