简体   繁体   中英

MVC Inheritance with Entity Framework: How do I scaffold a derived type?

Somebody please point me in the direction of sample code for .net Controllers and Views of derived classes...

(you might imagine that there would be plenty of resources, I'm not finding any... )

In the project I have a class derived from an Abstract class, EF maps a table-per-hierarchy db, and at runtime the DB look as expected with "UserTypeId" descriminator.

I've overcome initial compile and runtime errors ...

I tried scaffolding a controller based on the derived class, but that apparently leaves the controller ActionResult needing a cast like

Parent parent = db.AbstractPersons.OfType<Parent>().Single(p => p.AbstractPersonId == id);

further there's a runtime error in the Index view, so I updated the view with

@model IList<BeyondThemes.BeyondAdmin.Models.CardCore.Parent>

then the model doesn't have the properties needed to render correctly.

Seems a huge number of people must have resolved this, but the ContosoU example (and others) all end short of describing how to scaffold Controllers and Views for Models after creating an inheritance heirarchy.

I'm also interested to find out if EF6 treats Interfaces the same way as Abstract classes, and would appreciate to see examples for Table Per Type (and even Table per Concrete Class).

The latest error is 在此处输入图片说明

The error states that you are passing a Model of type IList<Parent> but you're treating the model as a single element, not a collection. If you plan on passing multiple items, you need to add a foreach statement to loop over the collection. If you want a single item, you need to change your Model definition to take only a single item (and pass that from the Controller to the View).

您从控制器传递的类型(父类)与视图的模型(IList)不匹配

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