WCF和实体框架4.1 POCO

时间:2012-03-05 06:38:09

标签: wcf entity-framework-4.1

`我有以下实体框架4.1模型。

public partial class AWLTR2Model : DbContext
{
    public AWLTR2Model()
        : base("name=AWLTR2Model")
    {
        Configuration.ProxyCreationEnabled = false;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Address> Addresses { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<CustomerAddress> CustomerAddresses { get; set; }
}

我有下面的POCO实体,它位于不同的名称空间AWLTR2.Entity

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
public partial class CustomerAddress
{

    public int CustomerID { get; set; }
    public int AddressID { get; set; }
    public string AddressType { get; set; }
    public System.Guid rowguid { get; set; }
    public System.DateTime ModifiedDate { get; set; }

    public virtual Address Address { get; set; }
    public virtual Customer Customer { get; set; }

}

我通过WCF服务(BasicHTTPBinding)公开了这些实体,但在我的客户端,我无法将子实体添加到父实体,如下所示。

  Customer c = new Customer
        {
            FirstName = "xx",
            CompanyName = "zz",
            EmailAddress = "xx@a.com",
            LastName = "yy",
            ModifiedDate = DateTime.Now,
            NameStyle = false,
            PasswordHash = "abcd",
            PasswordSalt = "abcd",
            Phone = "xx",
            rowguid = System.Guid.NewGuid()
        };

        CustomerAddress ca = new CustomerAddress 
        { 
            AddressID = 9 , 
            Customer = c , 
            AddressType ="Home Office", 
            ModifiedDate=System.DateTime.Now, 
            rowguid = System.Guid.NewGuid()
        };

客户端中缺少add方法,但我能够在不暴露WCF的情况下执行此操作。 如下所示。            c.CustomerAddresses.Add(CA);

我想写一个方法来一次性添加客户以及地址和客户地址。我使用具有对象上下文的实体framewor 4.0实现了相同的功能。但是无法在Dbcontext中这样做。

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

如果客户端上不存在Add方法,则很可能意味着您已将服务引用添加到WCF服务,但未更改集合的默认类型 - 默认为数组。

配置您的服务参考,在高级设置中,将“收集类型”更改为“列表”。