Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem Insert using Linq to Entity- many relationships

Posted on 2009-04-13
2
Medium Priority
?
1,242 Views
Last Modified: 2013-11-11
Hi there,
I have a problem using linq to Entity to perform a simple insert.
An example for my problem:
The tables:
gafProducts - n .. 1 - gafProcuctsType
gafProvices - 1 .. n - gafCities

And finally, gafProducts - n .. 1 - gafCities.

So, I did well when gafProducts had gafCities or gafProcutsType, the insert works, but when I have objects for these two tables (gafCities AND gafProductsType) the insert fails.

I already did it setting the EntityKey for the tables objects, and its works if I insert with gafCities data OR gafProductType data, not with both.

The exception:
Entities in 'DBEntities.gafCities participate in the 'FK_gafCities _gafProvinces relationship. 0 related gafProvinces were found. 1 gafProvinces is expected.

But, why this happens if 'gafProvinces' are related in 'gafCities' (look the include).


public void SaveProduct(gafProducts d)
        {
            using (DBEntities db = new DBEntities())
            {               
                if (d.gafProductsType != null)
                {
                    d.gafProductsType = db.gafProductsType .Where(w => w.productTypeId == d.gafProductsType .productTypeId ).First();                   
                }                
 
                if (d.gafCities != null)
                {                    
                    d.gafCities = db.gafCities .Include("gafProvinces").Where(w => w.cityId== d.gafCities .cityId).First();                                 
                }                
 
                db.AddTogafProducts(d);
                db.SaveChanges();                
            }
        }

Open in new window

0
Comment
Question by:fabiomenezes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 80 total points
ID: 24135454
I am not sure if this is the reason, but yo uare using two seperate DBEntities which often confuses L2E.

First, you are getting your initial 'gafProducts d' with a certain DBEntities, but then you ar emaking a new one in your method here - often that causes conflicts.

Instead, try reusing the one that you found your 'gafProducts d' with.  Maybe that will fix the conflict?
// you see now this miethod takes in the 'db' that you already called
// you will have to change how you call it.
// I always include my entity model in my method calls to prevent conflicts
public void SaveProduct(DBEntities db, gafProducts d)
{
  if (d.gafProductsType != null)
  {
    d.gafProductsType = db.gafProductsType .Where(w => w.productTypeId == d.gafProductsType .productTypeId ).First();                   
  }                
 
  if (d.gafCities != null)
  {                    
    d.gafCities = db.gafCities .Include("gafProvinces").Where(w => w.cityId== d.gafCities .cityId).First();                                 
  }                
 
  db.AddTogafProducts(d);
  db.SaveChanges();                
}

Open in new window

0
 

Accepted Solution

by:
fabiomenezes earned 0 total points
ID: 24143945
Hi!

I solved.

It appears that I fiil the tables entities before I use the linq clauses, so, I cannot use the gafProducts ('d' object) to performe conditions in the 'where'.

I need new objects, referencies of the tables that the object in the clauses are in.

For example:

 if (d.gafCities != null)
  {                    
    d.gafCities = db.gafCities .Include("gafProvinces").Where(w => w.cityId== d.gafCities .cityId).First
();                                

becames...

gafCities c = new gafCities();
c.cityId = 1;
 if (d.gafCities != null)
  {                    
    d.gafCities = db.gafCities .Include("gafProvinces").Where(w => w.cityId== c.cityId).First();                                
  }  
  }  

and that works...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question