Solved

Problem Insert using Linq to Entity- many relationships

Posted on 2009-04-13
2
1,240 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 20 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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: …

623 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