Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8761355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:15:55+00:00 2026-06-13T15:15:55+00:00

I am working on setting up a new project using Code First for entity

  • 0

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

Namely, I get an ‘Object reference not set to an instance of an object’ error anytime I attempt to build the silverlight client project which should generate the client proxy classes.

Just to be clear, this error is not while running or debugging the application, but when building it.

I have isolated that this only happens if I have any navigation properties/Foreign Keys defined on my Code First classes.

Any help tonight would be greatly 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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:15:56+00:00Added an answer on June 13, 2026 at 3:15 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on setting up NHibernate for a project and I have a few
I am working on my first NHibernate project, and have the following setup/requirement. I
I am fairly new to Servlet Filters and have basically joined a project using
I am working with the new cfbuilder and using ANT to push my code
I have a project in which I am using .nettiers generated code as my
I am using Java EE with Spring framework. I have been setting up a
I have a model as shown below (created using Model-First approach). A book is
I'm rather new to Powershell and am working on setting up my profile.ps1 file.
I'm working on an app (Written in C#) that have a setting to run
I am working on a project using JavaMail. I want to access my Gmail

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.