Solved

Problem Insert using Linq to Entity- many relationships

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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