Sunday, August 21, 2011

Entity Framework troubles on WCF

If you have this recurring error..

The underlying connection was closed: The connection was closed unexpectedly.

.., and has accompanying error message similar to this (debug WCF effectively):
There was an error while trying to serialize parameter http://tempuri.org/:OpenQuestionResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.Question_67CF6019AA09770B428CB66B12E25E67E057C1CE1AF042237C78DD56D4B495D9' with data contract name 'Question_67CF6019AA09770B428CB66B12E25E67E057C1CE1AF042237C78DD56D4B495D9:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

even you turned off ProxyCreation in your OnModelCreating of your Entity Framework mapping:

public class EfDbMapper : DbContext
{
   
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Question>().Property(x => x.RowVersion).IsRowVersion();

        modelBuilder.Entity<Question>().HasMany(x => x.Comments).WithRequired(x => x.Question).Map(x => x.MapKey("Question_QuestionId"));
        
        modelBuilder.Entity<Question>().HasMany(x => x.Answers).WithRequired(x => x.Question).Map(x => x.MapKey("Question_QuestionId"));

        modelBuilder.Entity<Answer>().HasMany(x => x.Comments).WithRequired(x => x.Answer).Map(x => x.MapKey("Answer_AnswerId"));

        this.Configuration.ProxyCreationEnabled = false;

    }
}


.., but the error still occurs, that mean ProxyCreationEnabled was reset to true. I noticed when I plug the NHibernate repository, there's no WCF error happening, by then we can infer that the error doesn't lie in WCF. It has something to do with the ORM (in this case, Entity Framework) woes.


I noticed, it's better to turn off proxy in constructor. There's no more dreaded underlying connection was closed after I moved the disabling of ProxyCreation to Entity Framework constructor


public class EfDbMapper : DbContext
{
    public EfDbMapper() 
    {
        this.Configuration.ProxyCreationEnabled = false;        
    }
    

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Question>().Property(x => x.RowVersion).IsRowVersion();

        modelBuilder.Entity<Question>().HasMany(x => x.Comments).WithRequired(x => x.Question).Map(x => x.MapKey("Question_QuestionId"));
        
        modelBuilder.Entity<Question>().HasMany(x => x.Answers).WithRequired(x => x.Question).Map(x => x.MapKey("Question_QuestionId"));

        modelBuilder.Entity<Answer>().HasMany(x => x.Comments).WithRequired(x => x.Answer).Map(x => x.MapKey("Answer_AnswerId"));


    }
}

No comments:

Post a Comment