English 中文(简体)
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.

问题回答

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.

You could save yourself a lot of work by using the Text Template Transformation Toolkit (T4) supported by EF4. I found this one after a good 12 hours of looking for a way around manually creating my POCOs and interfaces,

http://blogofrab.blogspot.com/2010/08/maintenance-free-mocking-for-unit.html

Besides providing a brilliant base for unit testing, it auto-generates navigational properties based on the relationships defined in your model.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签