Entity Framework: assign object to another property

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

  • Tongue
  • district
  • Description

These are the links between them:

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

If I get the following:

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 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

      

Thrown error:

System.InvalidOperationException: The relationship could not be defined because the EntitySet name 'MyEntities.Descriptions' is invalid for the role 'Region' association name 'MyEntities.District_Description'.

What am I doing wrong?

+2


source to share


2 answers


If myFetch

it was to be an instance of a 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);  

      

+1


source


Just what the message says: you specified the wrong objectset name.



  • Open your EDMX.
  • Open the Model Browser window.
  • Find Environment Object in Model Browser
  • Right click on it, select Properties
  • Note the correct name for the feature set
+2


source







All Articles