locked
Exception: The context needs the entity to have at least one key property before it can be tracked. RRS feed

  • Question

  •  

    DataServiceContext context = new DataServiceContext(new Uri("http://vs2008vpc/ADODOTNETDataService/DataService.svc"));
                context.Credentials = System.Net.CredentialCache.DefaultCredentials;
                context.MergeOption = MergeOption.AppendOnly;
                Categories c = new Categories();
                c.CategoryName = "NewCategory1";
                context.AddObject("Categories", c);    //Excepction here
                context.SaveChanges();

     

    anyone can help me?

    Tuesday, May 27, 2008 3:23 PM

Answers

  • Can you please send me how categories look likes? From the error, it seems that there was no key property defined on Categories.

     

    There are three ways of specifying the key properties (in the order of preference)

     

    1> you can specify a DataServiceKeyAttribute on the type and specify exactly which property/properties form the key for the type.

     

    2> If the type has a property named <typename>ID (case-sensitive), this property becomes the key for the type

     

    3> If the type has a property named ID (case-sensitive), this property becomes the key for the type.

     

    Can you make sure one of the these 3 rules is true on the Categories type?

     

    Thanks

    Pratik

    Tuesday, May 27, 2008 5:08 PM
    Moderator

All replies

  • Can you please send me how categories look likes? From the error, it seems that there was no key property defined on Categories.

     

    There are three ways of specifying the key properties (in the order of preference)

     

    1> you can specify a DataServiceKeyAttribute on the type and specify exactly which property/properties form the key for the type.

     

    2> If the type has a property named <typename>ID (case-sensitive), this property becomes the key for the type

     

    3> If the type has a property named ID (case-sensitive), this property becomes the key for the type.

     

    Can you make sure one of the these 3 rules is true on the Categories type?

     

    Thanks

    Pratik

    Tuesday, May 27, 2008 5:08 PM
    Moderator
  • I follow ur instruction and get another exception.

    now, I update my northwind db using the new release from codeplex, and this time I got the error message is : 403 Forbidden

    and I my sure I set the operation rule in the .svc codeside

     

    anyone can tell me what's the problem?

    thanks

     

    Thursday, May 29, 2008 4:50 AM
  • hello
    i am using ADO .Net entity framework and the the EDM was created by ADO .net entity framework designer tool
    and i have created ADO Data service and published it to my local IIS

        public class AstoriaService : DataService<NorthWindEntities>
        {
            public static void InitializeService(IDataServiceConfiguration config)
            {
                // Example for entity sets (this example uses "AllRead" which allows reads but not writes)
                config.SetEntitySetAccessRule("*", EntitySetRights.All);

                // Example for service operations
                config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);

                config.UseVerboseErrors = true;
            }

        }
    my NorthWindEntites is ADO Entity FrameWork Entities


    Using DataSerive

     DataServiceContext DataContext =  new DataServiceContext(new Uri(SERVICEURL));

    i try to retrive  Cateogries like this
      DataContext.CreateQuery<Categories>("/Categories").ToList();
    it is working good

    but when i want to add a new category i using AddObject function in my DataServiceContext

    the Categories is the same class which i using in the dataservice

    Categories category = new Categories();
    category.CategoryName = "test category";
    DataContext.MergeOption = MergeOption.AppendOnly;                       
    DataContext.AddObject("Categories", category ); // throw an execption here
    DataContext.SaveChanges();


    it gives an Excption
    "The context needs the entity to have at least one key property before it can be tracked. "

    plz i need a help plz
    Wednesday, August 6, 2008 5:05 PM
  •  

    Hi Mike,

     Can you please start a new thread so that this discussion is captured under the right subject ?

     How did you generate the client entities ? were they generated by DataSvcUtil.exe or webdatagen.exe ?

     It looks like the client entities do not contain all the information required for the update.

    Wednesday, August 6, 2008 8:47 PM
    Moderator
  • thanks man for your replay ,
     i create a new thread
    http://forums.microsoft.com/msdn/ShowPost.aspx?postid=3717864&isthread=true&siteid=1&authhash=2ff77808e02d19704d8da20571f9f1aed78ab5b4&ticks=633536780222898849

    i need a help ASAP
    Thursday, August 7, 2008 10:54 AM