Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架代码第一个客户和地址注册_C#_Entity Framework 4_Ef Model First - Fatal编程技术网

C# 实体框架代码第一个客户和地址注册

C# 实体框架代码第一个客户和地址注册,c#,entity-framework-4,ef-model-first,C#,Entity Framework 4,Ef Model First,一个用于客户,一个用于地址 所需功能当客户注册时,他们会在同一视图中输入个人详细信息,如姓名、电话以及地址详细信息 当前功能 目前,EF scaffolding提供了一个可供选择的地址下拉列表 当前代码 public class Customer { public int CustomerId { get; set; } public string FirstName { get; set; } [Required] public string Surname {

一个用于
客户
,一个用于
地址

所需功能当客户注册时,他们会在同一视图中输入个人详细信息,如姓名、电话以及地址详细信息

当前功能 目前,EF scaffolding提供了一个可供选择的地址下拉列表

当前代码

public class Customer
{
    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    [Required]
    public string Surname { get; set; }
    [Required]

    ...
    Customer Fields
    ...

    public int AddressId { get; set; }

    public virtual Address Address { get; set; }
}

public class Address
{
    [Key]
    public int AddressId { get; set; }

    ...
    Address Fields
    ...

    // Navigation Properties
    public virtual List<Customer> Customer { get; set; }
}
公共类客户
{
public int CustomerId{get;set;}
公共字符串名{get;set;}
[必需]
公共字符串姓氏{get;set;}
[必需]
...
客户字段
...
public int AddressId{get;set;}
公共虚拟地址{get;set;}
}
公共课堂演讲
{
[关键]
public int AddressId{get;set;}
...
地址字段
...
//导航属性
公共虚拟列表客户{get;set;}
}
在为数据库设定种子时,我可以按如下方式执行:

new List<Customer>
    {
        new Customer 
            { 
                ** Customer Fields ** , 
                Address = new Address { ** Address Fields ** }
            }
    }.ForEach(c => context.Customers.Add(c));
base.Seed(context);
新列表
{
新客户
{ 
**客户字段**,
地址=新地址{**地址字段**}
}
}.ForEach(c=>context.Customers.Add(c));
种子(上下文);
我的想法

我最初的想法是,我应该创建一个名为
CustomerWithAddress
的第三个数据模型,它本质上是
customer
address
模型的组合。这将允许我构建一个强类型视图

或者,控制器是否可以将2个模型传递给1个视图


我不知道这是否是解决这个问题的最好方法,或者实际上是否可能。有什么想法吗?

如果您的客户模型具有address属性,则您可以从视图中访问它。e、 g

@Html.DisplayTextFor(x => model.Address.Addressline1)
您的viewmodel想法很好,但在这种特殊情况下不需要


编辑:正如我下面的朋友所指出的,如果采用延迟加载,则可能需要手动加载Address属性。

这可能需要使用
。Include()
来实际加载
Address
属性;如果EF是延迟加载对象,那么当它到达视图时,
model.Address
可能为空。