0
votes

I am trying to add an object to my entity but it is giving me the error: "Not Found" here is my code:

    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();

I have tried "Cars" and "Car" both in AddObject but still didnt help.. my CARID column is a PKEY column in database.

Please help. Thanks.

1
What are the names of your entity sets? I.e., when you go to http://.../wcfservice1.svc, what are the names that show up? - Jen S
its name is showing as "Cars" - RHM
Hmm, and dtx.AddObject("Cars", car); is giving you the same error? And that error is a 404 from the server? Could you use a tool like Fiddler (or any other web traffic monitoring program) to see what the outgoing request URI (and payload) is? I would expect the URL to be /wcfservice1.svc/Cars, and I would also expect that if you go to that URL in a browser you shouldn't get a 404. Could you try that out? - Jen S
Yes I can browse that through browser and it is working fine but from the code AddObject throws the error. - RHM

1 Answers

0
votes

You may need to set the read/write settings on your DataService class

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

If you are using an ObjectContext then DataService<T> will know to use the EF WCF Data Services Provider. However if your T is anything but, (for example DbContext or DataContext), then by default WCF Data Services uses the reflection provider (which is readonly).

There is a hack to get the ObjectContext out of a DbContext, but if you need to use DataContext, nHibernate, or any other type of context, you will need to write a custom IDataServiceUpdateProviderimplementation.