Solved

Problem Insert using Linq to Entity- many relationships

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

How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 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