EF5 Code First and RIA Services Silverlight “Object reference not set to an instance of an object” error building client

╄→гoц情女王★ 提交于 2019-12-05 06:12:27

Your foreign key fields aren't mapped, thus they can't be interpreted by the proxy code generator (the piece of code called to build your proxy during compilation).
You should put in 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 to change your
public virtual List<Character> Characters { get; set; }
to
public virtual ICollection<Character> Characters { get; set; } , because I'm not sure if the proxy generator (and EF too) will map that list correctly.
EDIT:
I'm thinking that the EF Metadataprovider isn't supplying the correct attribute in description.
Put a KeyAttribute over the Character.CharacterId and Person.PersonID, also, add this line over Character.Person

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

and this one over Person.Characters

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

EDIT:
After chat with KitKat we finally found the problem. During proxy generation a call to Assembly.GetExportedTypes crashed complainig it need EF 4.1. Simple putting

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

in the related config did the tricks

Note: at this link ther's blog post from mine that better explains how to deal with EF5 Code first and WCF Ria Services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!