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

  • Home
  • SEARCH
  • 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 8405057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:45:13+00:00 2026-06-09T22:45:13+00:00

I have the following entities: public interface IMyEntity { [Key] int Id { get;

  • 0

I have the following entities:

public interface IMyEntity
{
    [Key]
    int Id { get; set; }
    IMyDetail MyDetail { get; set; }
    ICollection<IMyDetail> CollectionOfReferences { get; set; }
}

public interface IMyDetail
{
    [Key]
    int Id { get; set; }
    int IntValue { get; set; }
}

public class MyEntity : IMyEntity
{
    [Key]
    public virtual int Id { get; set; }
    public virtual IMyDetail MyDetail { get; set; }
    public virtual ICollection<IMyDetail> CollectionOfReferences { get; set; }
}

public class MyDetail : IMyDetail
{
    [Key]
    public virtual int Id { get; set; }
    public virtual int IntValue { get; set; }
}

I want to use EF CodeFirst to access the database and to create database schema. But CodeFirst doesn’t allow to use interface types for relations between entities. Therefore it doesn’t create relation between MyEntity and MyDetail. I can’t change interfaces therefore I can’t change the type of property to MyDetail instead of IMyDetail. But I know that the client of this model will use only one implementation of each interface.

I’ve found a workaround for properties of type IMyDetail. I can create a property of type MyDetail and explicitly implement property of interface:

    private MyDetail _myDetail;

    public virtual MyDetail MyDetail
    {
        get
        {
            return this._myDetail;
        }
        set
        {
            this._myDetail = value;
        }
    }

    IMyDetail IMyEntity.MyDetail
    {
        get
        {
            return this._myDetail;
        }
        set
        {
            this._myDetail = (MyDetail)value;
        }
    }

It works fine. But this solution doesn’t work with ICollection<IMyDetail> because I can’t cast it to ICollection<MyDetail>.

Are there any solutions for this?

  • 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-09T22:45:15+00:00Added an answer on June 9, 2026 at 10:45 pm

    An imperfect solution is to just merge these interfaces you want to persist into base classes and break down the underlying objects with subclasses. EF does support this, and if you go with Table Per Hierarchy (the default), you can sort all of the underlying subclassed objects by a shared property using a regular LINQ query from EF instead of having to get crafty and do things like write raw SQL or get multiple lists into memory and sort the union without the help of the DB, like you would with Cel’s solution of interfaces and adapters.

    You could also take the child/parent types of interfaces as generics, so that when the implementer uses concrete classes in the Db they can mostly use your interfaces, but tell EF to use concrete classes:

    public interface IParent<out TChild>
        where TChild : IChild
    {
        ICollection<TChild> Children { get; set; }
    

    Someone could create their Db classes like:

    public class Parent : IParent<Child>
    . . .
    

    But still use them like:

    IParent<IChild> parents = db.Parents.Include(p => p.Children).ToArray();
    

    Because the generic is marked out, the generic is covariant and therefore can take anything that meets the generic’s restrictions, including the above cast up the type tree to the IChild interface.

    That said, if you really want to persist interfaces, the right answer is probably to use NHibernate:
    How to map an interface in nhibernate?

    And some coders recommend you keep interfaces on entities in any ORM limited to a few shared properties, or risk misuse:
    Programming to interfaces while mapping with Fluent NHibernate

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

Sidebar

Related Questions

I have following entities: public class Product { [Key] public int Id{get;set;} //other properties
I have two entities, defined as following public class Corporation { public int Id{get;set;}
I have following two entities public class User { public int UserId { get;
I have the following 2 entities: public class Product { [Key] public int ID
I have the following basic entities: public class Basket { public List<Product> Products {get;set;}
I have the following Entities; Ticket contains a set of 0,N WorkOrder: @Entity public
I have the following entities: public class User { public virtual ICollection<Role> Roles {
I have the following entities in Entity Framework: public class School { public int
Consider the following Entities. public class Product{ int id; Date effectiveDate; Date expiryDate; Set<Inventory>
Lets say i have the following controller: // // GET: /Courses/Edit/5 public ActionResult Edit(int

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.