【问题标题】:Entity Framework creating foreign record实体框架创建外部记录
【发布时间】:2012-10-21 13:37:51
【问题描述】:

我有一个外键指向地址表的用户表。

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...但是,如果地址记录尚不存在(除了首先创建它)我该怎么办?可能以某种方式将它们指向地址视图,强制他们首先创建地址并将主键传回或将两个视图放在同一页面上?非常感谢。

【问题讨论】:

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


    【解决方案1】:

    在同一事务中(并在同一页面上)创建它们。例如:

    Details:
    --------
    First Name:  [____]
    Second Name: [____]
    etc.
    
    Address:
    --------
    Line 1:      [____]
    Line 2:      [____]
    City:        [____]
    State:       [____]
    Country:     [____]
    etc.     
    

    【讨论】:

    • Thnaks,能够使用您的建议做到这一点 -> @model Models.User 并使用 @Html.LabelFor(model => model.Address.StreetName) 添加地址
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多