Danish Shabbir
Danish Shabbir

Reputation: 13

Insert Data in Parent table and then inserting parent primary key in child table

I am doing my final year project and we are stated to follow object oriented approach during design and development.

My question is: I have a parent table User:

public partial class User

This table has its attributes.

Then I have a child table Advertiser:

public partial class Advertiser : User

I am using EF6. Now please tell me approach how to save data into the User table and then take the UserID and paste it into the Advertiser table.

Problem

When I do this

Advertiser.AdvertiserID = User.UserID;

db.SaveChanges();

Then it says provide all the values of Advertiser class which came into existence due to inheritance.

Upvotes: 1

Views: 941

Answers (2)

Win
Win

Reputation: 62260

If User and Advertiser have a relationship in database, you might have Advertiser in User POCO for one-to-one relationship.

public partial class User
{
   public User() {..}    
   ...
   public virtual Advertiser Advertiser { get; set; }
}

If so, you could just call SaveChanges once. They become transaction.

user.Advertiser = new Advertiser 
{
    // No need to assign value to AdvertiserID
    FirstName = "John",
    LastName = "Doe"
}; 
db.Users.Add(user);
db.SaveChanges();

Otherwise, they do not have relationship in database. You will have to save one after another.

db.Users.Add(user);
db.SaveChanges();

advertiser.AdvertiserID = user.UserID; // UserID is filled with newly created ID
db.Advertiser.Add(advertiser);
db.SaveChanges();

Upvotes: 1

Steve Py
Steve Py

Reputation: 34683

In your example, the relationship between advertiser and user isn't parent-child, it is inheritance. EF supports three models for inheritance: table per type, table per hierarchy, and table per concrete instance. To get a better understanding on how these are set up, and how their schema will look you can reference (https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph) which has links for all three, or Google EF and TPT/TPH/TPC for alternative examples. (DB-First)

In all casses, Advertiser's PK would be UserID. With considering inheritance vs. composition the question is whether the relationship is an "Is a" or a "Has a". Is an advertiser a user, or does an advertiser have a user? I would guess that Is-A is an accurate relationship.

In an Is-A relationship, all entities that "are" users would have PK's of "UserId". (whether there is a Users base table or not) In a Has-A relationship, such as an Advertiser has an Address, then you would consider relationships like a many-to-1 or 1-to-1 where Advertiser has an AddressID to point to an address record, or a many-to-many there is a joining table of UserID and AddressID to link the two together.

As long as you keep these relationships consistent, EF configuration should be pretty straight forward.

Upvotes: 0

Related Questions