0

Is it possible to have a foreign key mapping based on a specific column value.

I have the following entities.

public class Controller
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual List<ControllerDevice> ActiveDevices { get; set; }
    public virtual List<ControllerDevice> TamperedDevices { get; set; }
    public virtual List<ControllerDevice> IgnoredDevices { get; set; }
}

public class ControllerDevice
{
    public int Id { get; set; }
    public DeviceStatus Status { get; set; }

    public int ControllerId { get; set; }
    public int NetworkDeviceId { get; set; }

    public virtual Controller Controller { get; set; }
    public virtual NetowkDevice NetowkDevice { get; set; }
}

public class NetowkDevice
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

public enum DeviceStatus
{
    Active,
    Tampered,
    Ignored
}

Is it possible to have the ActiveDevices, TamperedDevices and IngoredDevices list be auto populated based on ControllerDevice DeviceStatus, or would I have to create three different tables for each list. IE ActiveControllerDevice, TamperedControllerDevices and IgnoredControllerDevices.

Please let me know if you require further explanation.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jethro
  • 5,896
  • 3
  • 23
  • 24

3 Answers3

0

Use single devices collection:

public class Controller
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual List<ControllerDevice> Devices { get; set; }
}

...and filter it, when you need to process or display devices with specific Status value:

controller.Devices.Where(d => d.Status == DeviceStatus.Active);

Several tables for each devices status, and/or devices hierarchy (theoretically, you can solve this problem with a TPH inheritance) is a way to hell, because instead of single entity ControllerDevice with a status you'll get three entity types (ActiveControllerDevice, TamperedControllerDevice and IgnoredControllerDevice), which is not corresponding to model.

Instead of changing status, the device will change its type, and you cannot do that in simple way.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Hi Dennis, I think I was trying to over complicate things, this looks like a much better solution. Thanks. – Jethro Jan 11 '13 at 08:55
0

Yes, you can do that. Enum support was introduced in Entity Framework 5, .Net Framework 4.5. In Entity Framework, an enumeration can have the following underlying types: Byte, Int16, Int32, Int64 , or SByte.

And you can filter like this:

context.ControllerDevices.Where(d => d.Status == DeviceStatus.Active);

More here: http://msdn.microsoft.com/en-us/data/hh859576.aspx

phnkha
  • 7,782
  • 2
  • 24
  • 31
0
public class TestContext : DbContext
{
   public TestContext()
   {
      Configuration.AutoDetectChangesEnabled = true;
      Configuration.LazyLoadingEnabled = true;
      Configuration.ProxyCreationEnabled = true;
      Configuration.ValidateOnSaveEnabled = true;
   }

   public virtual DbSet<NetowkDevice> NetowkDevices{ get; set; }
   public virtual DbSet<ControllerDevice> ControllerDevices{ get; set; }
   public virtual DbSet<Controller> Controlleres{ get; set; }
}

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d0443029-2175-4bde-a834-4f8dbf313201/

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

Community
  • 1
  • 1
zandi
  • 704
  • 5
  • 17