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