EF5 Code First and RIA Services Silverlight "Link object not installed on object instance" client create error

I am working on a new project using Code First for the framework 5 entity in silverlight using RIA services. I created a test project due to some problems that I encountered and will post the code below.

Namely, I get the error "Link to an object that is not installed on an instance of the object" when I try to create a silverlight client project that should generate client proxy classes.

To be clear, this error is not during the launch or debugging of the application, but during its creation.

I highlighted that this only happens if I have any navigation properties / foreign keys defined in first class classes.

Any help today would be appreciated.

public class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime? BirthDate { get; set; } public virtual List<Character> Characters { get; set; } } public class Character { public int CharacterId { get; set; } public int PersonId { get; set; } public virtual Person Person { get; set; } public string CharacterName { get; set; } } public class CharacterDbContext : DbContext { public DbSet<Person> Persons { get; set; } public DbSet<Character> Characters { get; set; } public CharacterDbContext() { if (HttpContext.Current == null) { Database.SetInitializer<CharacterDbContext>(null); } } } [EnableClientAccess] public class CharacterDbService : DbDomainService<CharacterDbContext> { #region Basic Methods for Person with the context property of Persons [Query] public IQueryable<Person> GetPersons() { return DbContext.Persons; } [Insert] public void InsertPerson(Person entity) { DbEntityEntry<Person> entityEntry = DbContext.Entry(entity); if (entityEntry.State != EntityState.Detached) { entityEntry.State = EntityState.Added; } else { DbContext.Persons.Add(entity); } } [Update] public void UpdatePerson(Person entity) { DbContext.Persons.AttachAsModified(entity, ChangeSet.GetOriginal(entity), DbContext); } [Delete] public void DeletePerson(Person entity) { DbEntityEntry<Person> entityEntry = DbContext.Entry(entity); if (entityEntry.State != EntityState.Deleted) { entityEntry.State = EntityState.Deleted; } else { DbContext.Persons.Attach(entity); DbContext.Persons.Remove(entity); } } #endregion #region Basic Methods for Character with the context property of Characters [Query] public IQueryable<Character> GetCharacters() { return DbContext.Characters; } [Insert] public void InsertCharacter(Character entity) { DbEntityEntry<Character> entityEntry = DbContext.Entry(entity); if (entityEntry.State != EntityState.Detached) { entityEntry.State = EntityState.Added; } else { DbContext.Characters.Add(entity); } } [Update] public void UpdateCharacter(Character entity) { DbContext.Characters.AttachAsModified(entity, ChangeSet.GetOriginal(entity), DbContext); } [Delete] public void DeleteCharacter(Character entity) { DbEntityEntry<Character> entityEntry = DbContext.Entry(entity); if (entityEntry.State != EntityState.Deleted) { entityEntry.State = EntityState.Deleted; } else { DbContext.Characters.Attach(entity); DbContext.Characters.Remove(entity); } } #endregion } 
+6
source share
1 answer

Your foreign key fields are not displayed, so they cannot be interpreted by the proxy code generator (a piece of code called to create your proxy at compile time).
You should put you dbcontext something like

  protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Character>() .HasRequired(x=> x.Person) .WithMany(x=> x.Characters) .HasForeignKey(x=> x.PersonId); } 

also, I suggest you change your
public virtual List<Character> Characters { get; set; }
on public virtual ICollection<Character> Characters { get; set; } public virtual ICollection<Character> Characters { get; set; } public virtual ICollection<Character> Characters { get; set; } , because I'm not sure that the proxy generator (and EF too) will display this list correctly.
EDIT:
I think that EF Metadataprovider does not supply the correct attribute in the description.
Put a KeyAttribute over Character.CharacterId and Person.PersonID, also add this line on top of Character.Person

 [Association("Character_Person", "PersonId", "PersonId", IsForeignKey = true)] 

and this character is over Person.Characters

 Association("Character_Person", "PersonId", "PersonId")]<br> 

EDIT:
After chatting with KitKat, we finally found a problem. During proxy generation, the call to Assembly.GetExportedTypes crashed, requiring EF 4.1. Simple placement

 <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> </dependentAssembly> </assemblyBinding> 

tricks were performed in the appropriate configuration

Note: this blog post is from mine, which better explains how to deal with EF5 Code first and WCF Ria Services

+6
source

All Articles