实体框架创建外国记录

时间:2012-10-21 13:37:51

标签: c# asp.net asp.net-mvc entity-framework

我有一个User表,外键指向地址表。

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; }
}

在视图中工作正常,即在创建新的“用户”记录时要求用户选择AddressID ...但是,如果地址记录尚不存在(除了先创建它),我该怎么办呢?有可能以某种方式将它们指向地址视图,强制它们首先创建地址并传回主键或将两个视图放在同一页面上?非常感谢。

1 个答案:

答案 0 :(得分:2)

在同一个交易中(并在同一页面上)创建它们。 E.g:

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

Address:
--------
Line 1:      [____]
Line 2:      [____]
City:        [____]
State:       [____]
Country:     [____]
etc.     
相关问题