views:

252

answers:

2

Hi,

I have these entities (this is just an abstraction I created for this post):

  • Language
  • District
  • Description

These are the references between them:

  • District * - 1 Language
  • Description * - 1 Language
  • District 1 - 1 Description

If I fetch like this:

var myFetch = from c in context.Districts
              where c.Id = 10
              select new { DistrictId = c.Id, Lang = c.Language };

and after that, I try to assign it to Description like this:

Description desc = Description.CreateDescription(0, "My description");
desc.DistrictReference.EntityKey = new EntityKey("MyEntities.Descriptions", "DistrictId", myFetch.DistrictId);
desc.Language = myFetch.Lang; //throws error

The error thrown is:

System.InvalidOperationException: The relationship cannot be defined because the EntitySet name 'MyEntities.Descriptions' is not valid for the role 'District' in association set name 'MyEntities.District_Description'.

What am I doing wrong?

+2  A: 

Just what the message says: You specified the wrong entity set name.

  1. Open your EDMX.
  2. Open the Model Browser window.
  3. Find the District entity in the Model Browser
  4. Right click it, choose "Properties"
  5. Note the correct Entity Set name
Craig Stuntz
No, that is not the case. And either way, District is not used inside a string, so I couldn've misspelled it.
veljkoz
Yes it is; you just haven't found it yet. *All* EF names eventually go back to strings (in the EDMX). Like I said, look at the mapping. Descriptions may be the EntitySet name you *intended,* but it's not what's in your model.
Craig Stuntz
Argh... I've put the EntityKey on the wrong side.... it should've been: desc.DistrictReference.EntityKey = new EntityKey("MyEntities.Districts", "Id", myFetch.DistrictId);Thanks!
veljkoz
+1  A: 

if myFetch were to be an instance of the class District you could do it programmatically:

desc.DistrictReference.EntityKey = new EntityKey(  
  String.Format(  
    "{0}.{1}",   
    myFetch.EntityKey.EntityContainerName,   
    myFetch.EntityKey.EntitySetName),   
  "DistrictId", 
  myFetch.DistrictId);  
mathijsuitmegen
I like this - it saves the trouble of misspelling
veljkoz