0

特定の列値に基づいて外部キーマッピングを行うことは可能ですか?

私は次のエンティティを持っています。

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
}

ActiveDevicesTamperedDevicesおよびIngoredDevicesリストをに基づいて自動入力することは可能ですControllerDevice DeviceStatusか、それともリストごとに3つの異なるテーブルを作成する必要がありますか。IE ActiveControllerDeviceTamperedControllerDevicesおよびIgnoredControllerDevices

さらに説明が必要な場合はお知らせください。

4

3 に答える 3

0

単一のデバイスコレクションを使用します。

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

    public string Name { get; set; }

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

Status...特定の値を持つデバイスを処理または表示する必要がある場合は、それをフィルタリングします。

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

デバイスのステータスやデバイスの階層(理論的には、TPHの継承でこの問題を解決できます)ごとにいくつかのテーブルがあります。これはControllerDevice、ステータスを持つ単一のエンティティの代わりに、3つのエンティティタイプ(、、ActiveControllerDeviceおよびTamperedControllerDeviceIgnoredControllerDeviceを取得するためです。 、モデルに対応していません。

ステータスを変更する代わりに、デバイスはそのタイプを変更します。これを簡単な方法で行うことはできません。

于 2013-01-11T08:31:31.213 に答える
0

はい、できます。列挙型のサポートは、Entity Framework 5、.NetFramework4.5で導入されました。Entity Frameworkでは、列挙型の基になるタイプは、Byte、Int16、Int32、Int64、またはSByteです。

そして、あなたはこのようにフィルタリングすることができます:

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

詳細はこちら: http: //msdn.microsoft.com/en-us/data/hh859576.aspx

于 2013-01-11T08:38:51.097 に答える
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/

エンティティフレームワーク4.1とMVC3で動的プロキシを有効または無効にする必要がありますか?

于 2013-01-11T08:43:50.173 に答える