Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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#_.net_Database_Mapping_Entity - Fatal编程技术网

C# 无法使用实体框架将对象映射到其他表

C# 无法使用实体框架将对象映射到其他表,c#,.net,database,mapping,entity,C#,.net,Database,Mapping,Entity,我有一个类,有这个字段 public int CustomerId { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } public string homeAddress { get; set; } public string Email { get; set; } public PhoneCatalog P

我有一个类,有这个字段

    public int CustomerId { get; set; }

    public string Firstname { get; set; }

    public string Lastname { get; set; }

    public string homeAddress { get; set; }

    public string Email { get; set; }
   
    public PhoneCatalog Phone { get; set; }
我想将Phone(mobilePhone、WorkPhone和HomePhone)保存在另一个名为Phone的表中。 我尝试了这个代码,但它不起作用`

 modelBuilder.Entity<Customer>().ToTable("Customer");
            modelBuilder.Entity<PhoneCatalog>().ToTable("Phone");
            modelBuilder.Entity<Customer>().
                HasOne<PhoneCatalog>(a => a.Phone).WithOne(b => b.Customer).HasForeignKey<PhoneCatalog>(a=>a.Email);`
modelBuilder.Entity().ToTable(“客户”);
modelBuilder.Entity().ToTable(“电话”);
modelBuilder.Entity()。
HasOne(a=>a.Phone)。WithOne(b=>b.Customer)。HasForeignKey(a=>a.Email)`
因为我想同时保存客户详细信息和他的电话,所以我使用电子邮件作为外键。你能帮我找到解决这个问题的办法吗? 提前感谢

Linq是一个查询(阅读),不用于写作。因此,当您执行linq时,您正在将结果读入一个对象(而不是两个)。为了能够写入两个表,您需要创建一个新的类MyObject,它有两个属性1)自定义2)Phone