WCF数据服务AddObject

时间:2013-06-02 21:19:11

标签: wcf wcf-data-services

我正在尝试向我的实体添加一个对象,但它给了我错误:“Not Found” 这是我的代码:

    DataServiceContext dtx = new DataServiceContext(new Uri("http://localhost/website2/wcfservice1.svc/"));
    dtx.Credentials = System.Net.CredentialCache.DefaultCredentials;
    ServiceReference1.Car car = new ServiceReference1.Car();
    car.CarName = "aaa";
    car.CarModel = "111";

    dtx.AddObject("Car", car);

    dtx.SaveChanges();

我在AddObject中尝试了“汽车”和“汽车”,但仍然没有帮助..我的CARID列是数据库中的PKEY列。

请帮忙。感谢。

1 个答案:

答案 0 :(得分:0)

您可能需要在DataService类上设置读/写设置

public class FooDataService : DataService<MyContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Cars", EntitySetRights.All);
    }
}

如果您使用ObjectContext,那么DataService<T>将知道使用EF WCF数据服务提供商。但是,如果您的T不是,(例如DbContextDataContext),则默认情况下,WCF数据服务使用反射提供程序(只读)。

有一个hack可以从ObjectContext中获取DbContext,但是如果你需要使用DataContext,nHibernate或任何其他类型的上下文,你需要编写自定义IDataServiceUpdateProviderimplementation.

相关问题