Reputation: 710
Hi I am using TPT inheritance in EF. I have BaseClass=AspNetUser. SubClasses =Employer and Employee. I want to add a Document class which has a 1-M relationship with AspNetUser, so that you can only see your own documents. When I try to add a document from the Employer screen for example, i want to add an item to the Employer Documents Collection. It is not adding a record to the database though. What is the correct way to add an item to this collection. Do you have to navigate to the base class? I want to be able to do something like this
var avatar = new Document {...}
employer.Documents = new List<Document> { avatar };
db.Entry(employer).State = EntityState.Modified;
db.SaveChanges();
Upvotes: 1
Views: 31
Reputation: 23220
First in the AspNetUser
class, initialize the collection Documents
in the constructor like code below. If you're using Database First approach then this already be done by the EDMX:
public AspNetUser()
{
this.Documents = new List<Documents>();
}
Second, to add a new document in your Documents
collection just do this:
var avatar = new Document {...}
employer.Documents.Add(avatar);
db.SaveChanges();
Make sure that you retrieve your Employee
instance from your DbContext
.
Upvotes: 1