This is a migrated thread and some comments may be shown as answers.

NoSuchObjectException with NOT NULL foreign key

3 Answers 97 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Fredrik Lindros
Top achievements
Rank 1
Fredrik Lindros asked on 09 Sep 2013, 12:31 PM
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

3 Answers, 1 is accepted

Sort by
0
Boris Georgiev
Telerik team
answered on 10 Sep 2013, 12:58 PM
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.
0
Andrey
Top achievements
Rank 1
answered on 15 Jan 2014, 11:41 AM
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
0
Boris Georgiev
Telerik team
answered on 20 Jan 2014, 11:06 AM
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!
Tags
Development (API, general questions)
Asked by
Fredrik Lindros
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Andrey
Top achievements
Rank 1
Share this question
or