1

我对 EF Code First 方法完全陌生,我遇到了一些麻烦。我真的需要你的帮助。

我有 3 节课,如下所示

 [Table("Customers")] 
public class Customer
{

    public long Id { get; set; }
    public string Name { get; set; }
    public string MiddleName { get; set; }
    public string Surname { get; set; }
    public string FirmName { get; set; }
    public string ShortName { get; set; }


    public virtual ICollection<CustomerEmail> CustomerEmails { get; set; }
    public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }




}

 [Table("CustomerEmail")]
public class CustomerEmail
{

    public long Id { get; set; }
    public string Email { get; set; }
    public DateTime RecordTime { get; set; }


    public virtual Customer Customer { get; set; }
}

 [Table("CustomerAddress")]
public class CustomerAddress
{
    public long Id { get; set; }
    public Int16 CountryCode { get; set; }
    public Int16 CityCode { get; set; }
    public string City { get; set; }
    public string Town { get; set; }
    public string District { get; set; }
    public string Street { get; set; }
    public string PostalCode { get; set; }


    public virtual  Customer Customer { get; set; }


}


 public class MyDbContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }
    public DbSet<CustomerEmail> CustomerEmails { get; set; }
    public DbSet<CustomerAddress> CustomerAddresses { get; set; }

}

我的问题

1)当我运行应用程序时,它找不到与客户相关的表,即 CustomerAddress 和 CustomerEmail。它给出了一个错误,说“Customer_Id 没有在 CustomerEmail 和 CustomerAddress 表中退出”。我认为这些模型之间的关系有问题。我需要你的有助于改善这些关系。

2)获取客户数据没有问题。但是它带来了所有数据。例如,我在客户表中有 10.0000 行,它在我的上下文类中带来了 10.000 行。这不好。我不希望它带来所有数据首先。我想在查询时只带相关数据。

伙计们,我真的很快需要你的帮助。谢谢...

4

2 回答 2

2
  1. Due you are not following the EF foreign key conventions, you will need to set those relations by fluent-mapping into your DbContext class. Look for a blog about fluent-mapping to figureout how to make one-to-one relations

  2. Maybe you are performing a linq query without a where clause. Please put some code related with this item.

于 2012-04-28T22:37:47.453 回答
1
[Table("CustomerAddress")]                     
public class CustomerAddress                     
{                         
public long Id { get; set; }  
public long CustomerId { get; set; }                         
public Int16 CountryCode { get; set; }                          
public Int16 CityCode { get; set; }                          
public string City { get; set; }                         
public string Town { get; set; }                         
public string District { get; set; }                          
public string Street { get; set; }                          
public string PostalCode { get; set; }                                                                      
public virtual  Customer Customer { get; set; 
}


 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {


        modelBuilder.Entity<CustomerAddresses>()
        .HasKey(a => a.CustomerId)
        .HasRequired(a => a.Customer)
        .WithMany(b => b.CustomerAddresses)
        .HasForeignKey(a => a.Id);        
    }

当我查询 context.Customers.Find(3);

Customer 的 CustomerAddresses 属性中应该有 2 行,但只有一行是第一行。为什么它只带来一行?我试图建立 1-n 关系

于 2012-05-16T13:25:31.033 回答