简体   繁体   中英

Creating a child object in ASP.NET MVC3

I created a class model using Entity Framework 4, based on my (empty) database, and followed this tutorial to set up a table per type inheritance, with the Student and Professor inheriting the abstract Person class.

Everything worked fine and then I went ahead and created the Student controller and the generated code for creating students isn't working. I don't have access to my code now, so I can't paste it, but the Create function is something like this:

function create(Student student){
    db.person.AddObject(student);
    db.person.SaveChanges();
}

This is giving me an error saying the Student can't be added because there's no Person in the database with a corresponding key. I'm very new to asp.net, but I believe the database context isn't inserting a person before trying to insert into the student table.

I'm sorry I can't provide more details like pics of the DB design or the actual code, but this is the best I can do right now.

Thanks!

look now

datacontext    mainTable   subTable   what is needed to do.

   "db"     .  "person"  . "student" . blablabla

db as far as i understand is ur database context and person is ur table and you are calling a sub table "student" which needs to have a relation with "person". so in ur database make sure you got a key for your "person" and "student" tables and make sure u got a relation between them.

if u can give more info i can help further but right now based on ur information this is the only knowledge i can share..

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