0

I have an interface:

public interface ILanguageEntity
{
    int Id { get; set; }

    string Name { get; set; }

    string Code { get; set; }

    string Culture { get; set; }

    string LocalName { get; set; }

    bool IsRightToLeft { get; set; }
}

And I've implemented that as a Entity like this:

public class LanguageEntity : ILanguageEntity
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Code { get; set; }

    public string Culture { get; set; }

    public string LocalName { get; set; }

    public bool IsRightToLeft { get; set; }
}

In my service I want to have a property for this entity which returns its DbSet like this:

public class LanguageService : ILanguageService
{
    public ApplicationContext Context { get; set; }

    public DbSet<ILanguageEntity> DbSet { get { return Context.Set<LanguageEntity>(); } }

}

But it's not possible because I'm returning a DbSet<LanguageEntity> type while I must return a DbSet<ILanguageEntity> type as compiler says. What should I do?

Saeed Hamed
  • 732
  • 2
  • 10
  • 28
  • Just curious, Why you need to have more interface for your model LanguageEntity? – cuongle May 04 '14 at 05:00
  • Because I'm trying to make my project modular and I need to access the LanguageEntity from the other modules, and other modules doesn't know where the LanguageEntity is. However they know how to use LanguageService. – Saeed Hamed May 04 '14 at 05:06

1 Answers1

0

well i assume that ApplicationContext should have atleast one ILanguageEntity DbSet

Here is what i came up with :

public class LanguageService : ILanguageService
{
    public ApplicationContext Context { get; set; }

    public DbSet<ILanguageEntity> DbSet
    {
        get
        {
            return Context.LanguageEntities;
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

public class ApplicationContext : DbContext
{
    public DbSet<ILanguageEntity> LanguageEntities { get; set; }
}

public interface ILanguageService
{
    DbSet<ILanguageEntity> DbSet { get; set; }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • If I use an interface instead of class for DbSet, the migration will returns and error while it tries to update the database. this is the error: The type 'RPSP.Framework.IEntities.Language.ILanguageEntity' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject. – Saeed Hamed May 04 '14 at 05:11