Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an Interface with a navigation property

I am trying to setup a project using Entity Framework 4, POCO, and Code-Only.

Is it possible in entity framework for type of a navigation property to be an interface?

I have a "Task" class. A Task can be assigned to a user or a group each of which are represented by a separate class and stored in separate tables. The classes look something like this:

public class User : IAssignable
{
    public string Name { get; set; }
    public int ID { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

public class Group : IAssignable
{
    public string Name { get; set; }
    public int ID { get; set; }
    public string Manager { get; set; }
    public string Department { get; set; }
}

public class Task
{
    public string Title { get; set; }
    public DateTime DueDate { get; set; }
    public string Details { get; set; }
    public IAssignable AssignedTo { get; set; }
}

Is there a way to may the AssignedTo property as a navigation property in entity framework? I assume there will have to be some type of discriminator for EF to know if it needs to look in the Users table or the Groups table but I can figure out the mapping using Code-Only or EDMX.

like image 246
TonyB Avatar asked Jan 06 '10 22:01

TonyB


People also ask

Can we use properties in interface?

An interface may define a default implementation for members, including properties. Defining a default implementation for a property in an interface is rare because interfaces may not define instance data fields.

What do you mean by navigation property?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

Can we use property in interface C#?

Interfaces can contain properties and methods, but not fields/variables. Interface members are by default abstract and public. An interface cannot contain a constructor (as it cannot be used to create objects)

CAN interface have variables in C#?

In C#, you are allowed to create a reference variable of an interface type or in other words, you are allowed to create an interface reference variable. Such kind of variable can refer to any object that implements its interface.


2 Answers

you can use interface in navigation property, take a look at this solution as it's the same as question: How to use interface properties with CodeFirst

like image 177
Ehsan Avatar answered Oct 04 '22 22:10

Ehsan


I know this is an old question, but no, there is no feature of Entity Framework (even the latest version 6) that allows you to map a navigation property with an interface type.

You could, however, map multiple navigation properties with concrete types (and a constraint that only one may be set) and provide an unmapped property of your interface type which coalesces the concrete navigation properties into a single property. Unfortunately, this may make your queries more complex because certain queries will need to know which concrete navigation properties to reference (and you can't query against your unmapped interface property).

There is significant complexity around support for polymorphic navigation properties. Consider what would have to happen in order to query your original AssignedTo property if you assume it's mapped to a column such as AssignedToId int. You'd have to union or join both User and Group entity sets and hope that a given AssignedToId appears in just one of them. This is the approach used by the Table-Per-Concrete (TPC) type mapping, but it only works with class inheritance (not interfaces) and careful planning for generating distinct ids across the participating types.

like image 23
Michael Petito Avatar answered Oct 04 '22 23:10

Michael Petito