简体   繁体   English

EF5 Code First和RIA Services Silverlight“对象引用未设置为对象的实例”错误构建客户端

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

I am working on setting up a new project using Code First for entity framework 5 in silverlight using RIA services. 我正在使用Code First为使用RIA服务的silverlight中的实体框架5建立一个新项目。 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. 也就是说,每当我尝试构建应该生成客户端代理类的silverlight客户端项目时,我都会得到一个“对象引用未设置为对象的实例”错误。

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. 我已经发现,只有在我的Code First类中定义了任何导航属性/外键时才会发生这种情况。

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
}

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 你应该把你的DbContext

 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; } public virtual ICollection<Character> Characters { get; set; } , because I'm not sure if the proxy generator (and EF too) will map that list correctly. public virtual ICollection<Character> Characters { get; set; } ,因为我不知道,如果代理生成器(和EF太)将正确映射该列表。
EDIT: 编辑:
I'm thinking that the EF Metadataprovider isn't supplying the correct attribute in description. 我认为EF Metadataprovider没有在描述中提供正确的属性。
Put a KeyAttribute over the Character.CharacterId and Person.PersonID, also, add this line over Character.Person KeyAttribute放在Character.CharacterId和Person.PersonID上,同时在Character.Person上添加这一行

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

and this one over Person.Characters 而这一个超过Person.Characters

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

EDIT: 编辑:
After chat with KitKat we finally found the problem. 在与KitKat聊天后,我们终于找到了问题所在。 During proxy generation a call to Assembly.GetExportedTypes crashed complainig it need EF 4.1. 在代理生成期间,对Assembly.GetExportedTypes的调用崩溃了,它需要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 注意:在此链接中我的博客文章中更好地解释了如何首先处理EF5代码和WCF Ria服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Silverlight 3 Ria服务参考 - Silverlight 3 Ria Services reference 将对WCF服务的引用添加到Silverlight 4项目时,显示“对象引用未设置为对象的实例”错误 - “Object reference not set to an instance of an object” error shows when adding a reference to a WCF service to a Silverlight 4 project Silverlight添加到列表 <T> 抛出未设置为对象实例的对象引用 - Silverlight adding to List<T> throws Object reference not set to an instance of an object Silverlight / RIA服务/ EF-自定义对象 - Silverlight / RIA Services / EF - Custom Objects 如何直接从Silverlight WCF RIA域服务获取对象 - How to directly get object from Silverlight WCF RIA Domain Services Silverlight RIA服务-客户端中服务器端项目的引用名称空间 - Silverlight RIA Services - reference namespace from server side project in client side EF-&gt; RIA服务-&gt; Silverlight,我可以显示验证错误消息,但仍保存更改 - EF -> RIA Services -> Silverlight, Can I show validation error messages but still Save changes Silverlight WCF RIA服务项目中的代码生成 - Code Generation in a Silverlight WCF RIA Services Project Silverlight应用程序中的RIA服务代码生成 - RIA Services Code Generation in Silverlight application Ria服务:简单&gt;&gt; object &lt;&lt;属性/字段未出现在客户端上 - Ria services: simple >>object<< properties/fields not appearing on the client side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM