Solved

Problem Insert using Linq to Entity- many relationships

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now