Adam Bilinski
Adam Bilinski

Reputation: 1198

Entity Framework creating foreign record

I have a User table with foreign key pointing to the Address Table.

public User()
{

}

public int UserID { get; set; }
public string ReferenceNumber { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public bool IsEnabled { get; set; }
public string TelephoneNumber { get; set; }
public string FaxNumber { get; set; }
public bool IsAccountant { get; set; }
public int AddressID { get; set; }
public virtual Address Address { get; set; }
}

That's working fine in the view i.e. the user is asked to select AddressID when creating new 'User' record... However, what can I do if the Address record does not exist yet (apart from creating it first) is it possible to somehow point them to the Address View, force them to create Address first and pass the primary key back or put both views on the same page? Many thanks.

Upvotes: 1

Views: 91

Answers (1)

Paul Fleming
Paul Fleming

Reputation: 24526

Create them in the same transaction (and on the same page). E.g:

Details:
--------
First Name:  [____]
Second Name: [____]
etc.

Address:
--------
Line 1:      [____]
Line 2:      [____]
City:        [____]
State:       [____]
Country:     [____]
etc.     

Upvotes: 2

Related Questions