web方法WCF中的返回请求



我有Web服务和一个方法,它使用实体框架向我的表"Customer"发出请求。

我想返回我的请求结果:

[WebMethod]
public Customer MyMethod(int id)
{
    Model model = new Model();
    Customer customer = new Customer();
    try
    {
        customer = model.Customer.Where(x => x.Name == id).First();
    }
    catch (Exception ex)
    {
        throw new System.NullReferenceException(ex.Message, ex.InnerException);
    }
    return customer;
}

我的班级客户(由EF生成):

[Table("Customer")]
public partial class Customer
{
    public int Id { get; set; }
    [StringLength(50)]
    public string Name { get; set; }
    public int? Adress_id { get; set; }
    public virtual Adress Adress { get; set; }
}

类别地址(由EF生成):

[Table("Adress")]
public partial class Adress
{
    public Adress()
    {
        Customer = new HashSet<Customer>();
    }
    public int Id { get; set; }
    [Column("Adress")]
    [StringLength(255)]
    public string Adress1 { get; set; }
    [StringLength(50)]
    public string Town { get; set; }
    public virtual ICollection<Customer> Customer{ get; set; }
}

但是当我用SoapUI调用我的方法时,我没有答案。如果我删除了属性虚拟Adress,那么我有一个答案,但我需要取回地址(联合)

感谢

如果您想显式加载一个相对属性作为查询的一部分,您可以通过使用Include扩展方法的Eaglely Loading来实现:

 customer = model.Customer.Include(c=>c.Address).Where(x => x.Name == id).First();

您也可以使用显式加载:

 customer = model.Customer.Where(x => x.Name == id).First();
 context.Entry(customer).Reference(p => p.Address).Load();

现在我建议您在数据库中检查该关系。如果您没有使用Fluent Api来配置该关系,则EF不知道Address_id是该关系的FK。如果外键属性命名为[Target Type Key Name][Target Type Name] + [Target Type Key Name],[Navigation Property Name] + [Target Type Key Name],则会按照约定发现它。覆盖此约定的一种方法是使用ForeignKey属性。因此,首先检查DB中的关系,然后检查是否有与要加载的Customer相关的Address。您应该能够使用我上面提出的两种变体之一加载Address导航属性。

由于virtual属性的原因,无法序列化模型以发送它们。您在customer中的地址引用了customers,它再次引用了address。。。这是一个无限循环。因此,您可以使用DTO(数据传输对象)类来传输数据。对于您的Customer型号,它看起来是这样的:

public class CustomerDTO
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int? Adress_id { get; set; }
  public Adress Adress { get; set; }      
}

在通过WCF发送之前,必须将Customer转换为CustomerDTO对象。你甚至可以用linq:在一个语句中做到这一点

var obectToTransfer = db.Customers.Where(c => c.Id == 5)
                                  .Select(c => new CustomerDTO
                                  {
                                     Id = c.Id
                                     ...
                                  }).FirstOrDefault();

我删除了类客户中的关键字virtual:

[Table("Customer")]
public partial class Customer
{
    public int Id { get; set; }
    [StringLength(50)]
    public string Name { get; set; }
    public int? Adress_id { get; set; }
    public Adress Adress { get; set; }
}

我删除了类Adress中的构造函数和属性ICollection<Customer> Customer

[Table("Adress")]
public partial class Adress
{
    public int Id { get; set; }
    [Column("Adress")]
    [StringLength(255)]
    public string Adress1 { get; set; }
    [StringLength(50)]
    public string Town { get; set; }
}

我的查询:customer = model.Customer.Include("Adress").Where(x => x.Id == 2).First();

它有效!

我的回应:

<a:Adress>
   <a:Adress1>101 Madison Ave</a:Adress1>
   <a:Id>1</a:Id>
   <a:Town>New York</a:Town>
</a:Adress>
<a:AdressId>1</a:AdressId>
<a:Name>John</a:Name>
<a:Id>2</a:Id>

但它并不实用

最新更新