none
The type <a interface> was not mapped. RRS feed

  • Question

  • Hi,

    Using CodeFirst I want to setup Entity Framework using "interfaced POCOs" with a final goal to only use a Interface  for my entities in the business logic.

    Here is what I try to do:

    public interface ISand
    {
        int SandId { get; set; }
        string Name { get; set; }
        int GranulationMicrometer { get; set; }
        DateTime CreationTimeStamp { get; set; }
    }
    
    public class Sand : ISand
    {
        public int SandId { get; set; }
        public int GranulationMicrometer { get; set; }
        public string Name { get; set; }
        public DateTime CreationTimeStamp { get; set; }
    }
    
    public class MyContext : DbContext
    {
        public DbSet<ISand> Sand { get; set; }
    }

    Doing this, I get the InvalidOperationException "The type 'Common.Interfaces.ISand' was not mapped."

    My Question is: It this approach possible and if yes, what is it I am doing wrong to get this exception?

    Thanks for any help or hints.

    Friday, April 19, 2013 6:26 AM

Answers

  • It is not a question of being fancy. It is more of a architectural question to have a good abstraction between data management and data usage. And don't be that pessimistic. I learned, that where is a will there is a way. I currently am on a way to achieve my goal. It's not elegant, but starts to get me where I want to be. (see below)

    Still, I appreciate any Help and Hints, that help me to achieve my goal perhaps a little more elegant ;-)

    Regards
    Rainer

    // A (sample) "Interfaced" POCO
    public class MixerRecipe : IMixerRecipe
    {
        private List<SandRecipe> mixerRecipeSands;
    
        public MixerRecipe()
        {
            this.mixerRecipeSands = new List<SandRecipe>();
        }
    	
    	//... some other properties ...
    	
        public virtual List<SandRecipe> SandRecipesLocal
        {
            get
            {
                return this.mixerRecipeSands;
            }
    
            set
            {
                this.mixerRecipeSands = value;
            }
        }
    
    	[NotMapped]
        public List<ISandRecipe> SandRecipes
        {
            get
            {
                return new List<ISandRecipe>(this.mixerRecipeSands);
            }
        }
    }
    //======================= The Context ==============================
    public class MyContext : DbContext
    {
        public DbSet<Sand> Sand { get; set; }
        public DbSet<SandRecipe> SandRecipe { get; set; }
        public DbSet<MixerRecipe> MixerRecipes { get; set; }    
    }
    
    // ======================= The Repository ==========================
    public class Repository : IDisposable
    {
        private readonly MyContext context;
    
        public Repository()
        {
            this.context = new MyContext();
        }
    
        public void Dispose()
        {
            if (this.context != null)
            {
                this.context.Dispose();
            }
        }
    
        public IEnumerable<IMixerRecipe> GetKnownMixerRecipes()
        {
            return this.context.MixerRecipes;
        }
    }

    Tuesday, April 23, 2013 8:05 AM

All replies


  • An interface can't be a property. An Interface is a contract  between two objects/classes or two concrete classes.

    One class/object implements the Interface,  and the  class/object uses the implementation implemented between the two object/class. T

    What you are doing is never going to work. An Interface is not a class with public properties. An Interface can't be an instance of an object/concrete class.

    Friday, April 19, 2013 11:04 AM
  • Well but a property can be a type of a interface.

    class SandBox
    {
        public ISand Sand {get; set;}
    }
    
    ISand sand = new Sand();
    SandBox sandBox = new SandBox();
    sandBox.Sand = sand;

    And this is what I am looking for:

    class Repository
    {
        ..... 
    
        public IEnumerable<ISand> GetSands()
        {
            return context.Sand;
        }
    }


    Friday, April 19, 2013 12:03 PM

  • Well you are getting just a little too fancy for the ORM. You can't do it you where using the Entity Data Model. I don't see how you can accomplish it with some class a (DTO/POCO) implementing an interface.

    Friday, April 19, 2013 5:11 PM
  • It is not a question of being fancy. It is more of a architectural question to have a good abstraction between data management and data usage. And don't be that pessimistic. I learned, that where is a will there is a way. I currently am on a way to achieve my goal. It's not elegant, but starts to get me where I want to be. (see below)

    Still, I appreciate any Help and Hints, that help me to achieve my goal perhaps a little more elegant ;-)

    Regards
    Rainer

    // A (sample) "Interfaced" POCO
    public class MixerRecipe : IMixerRecipe
    {
        private List<SandRecipe> mixerRecipeSands;
    
        public MixerRecipe()
        {
            this.mixerRecipeSands = new List<SandRecipe>();
        }
    	
    	//... some other properties ...
    	
        public virtual List<SandRecipe> SandRecipesLocal
        {
            get
            {
                return this.mixerRecipeSands;
            }
    
            set
            {
                this.mixerRecipeSands = value;
            }
        }
    
    	[NotMapped]
        public List<ISandRecipe> SandRecipes
        {
            get
            {
                return new List<ISandRecipe>(this.mixerRecipeSands);
            }
        }
    }
    //======================= The Context ==============================
    public class MyContext : DbContext
    {
        public DbSet<Sand> Sand { get; set; }
        public DbSet<SandRecipe> SandRecipe { get; set; }
        public DbSet<MixerRecipe> MixerRecipes { get; set; }    
    }
    
    // ======================= The Repository ==========================
    public class Repository : IDisposable
    {
        private readonly MyContext context;
    
        public Repository()
        {
            this.context = new MyContext();
        }
    
        public void Dispose()
        {
            if (this.context != null)
            {
                this.context.Dispose();
            }
        }
    
        public IEnumerable<IMixerRecipe> GetKnownMixerRecipes()
        {
            return this.context.MixerRecipes;
        }
    }

    Tuesday, April 23, 2013 8:05 AM
  • Hi Rainer,

    Thanks for your solution.

    EF doesn't work with interface.

    http://stackoverflow.com/questions/7431756/wrapping-dbsettentity-with-a-custom-dbset-idbset

    Here is also an article about repository pattern for you to see whether it is useful:

    http://www.codeproject.com/Articles/207820/The-Repository-Pattern-with-EF-code-first-Dependen

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 24, 2013 10:01 AM
    Moderator