NoSuchObjectException with NOT NULL foreign key

4 posts, 0 answers
  1. Fredrik Lindros
    Fredrik Lindros avatar
    13 posts
    Member since:
    Aug 2012

    Posted 09 Sep 2013 Link to this post

    Hello

    I'm having some problems with my associations based on NOT NULL foreign keys.
    I am using a "Database First" model and i have put together a simple example with 2 entities.

    ORDER has a foreign key to CUSTOMER which is not null

    Is this the expected behavior?
    ////////////////////////////////////////////////////////////////////////////////////////////////
    //This works fine
    using (EntitiesModel1 context = new EntitiesModel1())
    {
        var aCustomer = context.CUSTOMERs.Where(c => c.CustomerNumber == "1000").First();
        var anOrder = new ORDER();
        anOrder.CUSTOMER = aCustomer;
        anOrder.OrderNumber = "Ordernumber 1000";
        context.Add(anOrder);
        context.SaveChanges();
    }
     
     
    ////////////////////////////////////////////////////////////////////////////////////////////////
    //This throws a Telerik.OpenAccess.Exceptions.NoSuchObjectException
    //Message=No row for WpfApplication12.CUSTOMER ('CUSTOMER') GenericOID@a6a45507 CUSTOMER Id=0
    using (EntitiesModel1 context = new EntitiesModel1())
    {
        var aCustomer = context.CUSTOMERs.Where(c=>c.CustomerNumber == "1000").First();
        var anOrder = new ORDER();
        context.Add(anOrder);
        anOrder.CUSTOMER = aCustomer; //Throws exception
        anOrder.OrderNumber = "Ordernumber 1000";
        context.SaveChanges();
    }



    Best regards Fredrik
  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 10 Sep 2013 Link to this post

    Hello Fredrik,

    This is the expected behavior. In the first case, when you set the navigation property, the foreign key column/property takes the primary key value from the navigation property. In the second case, you are trying to add an order in the context, with null as value for the foreign key, and after this you are trying to modify the value, which is not possible. You should first set the foreign key value and then you should add the object to the context.
     
    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  3. DevCraft banner
  4. Andrew Lastochkin
    Andrew Lastochkin avatar
    14 posts
    Member since:
    Sep 2012

    Posted 15 Jan 2014 Link to this post

    Hello!

    When I have artificial fields, I need to add object to the context at first. 

    I have 2 questions:
    1) What to do, if I have normal static type with several artificial fields? 
    2) Why is associated record being fetched from the database? I do not need it, I simply set the value of FK identity field without accessing this associated record. How can I change this behavior?

    With respect, Andrey
  5. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 20 Jan 2014 Link to this post

    Hi Andrew,

    About the artificial types, I will recommend you to take a look at the Consuming Types Defined During Runtime sample in Telerik OpenAccess SamplesKit. The sample application demonstrates how to use Telerik OpenAccess ORM to manage the creation of artificial types, fields and entities.

    By default the associated records are lazy lodaded from the database(which means that they are loaded when you try to access them), unless you explicitly set to be loaded with the primary entity loaded from the database. If you want to change the default behavior you could specify your own fetch strategy. You can refer to this articles how to use Fetch Strategies. Also you can take a look at the Context API sample in Telerik OpenAccess Samples Kit, there is test section to show how to use Fetch Strategies.

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top