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.
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.
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.
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)
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With