0

モデルのプロパティとしてデータソースを動的に選択してモデルを作成する必要があります。ローカルとリモートのデータを選択するための依存性注入

class MyModel{ 
public int MyModelId {get; set;} 
... 
public int PropertyId {get;set;} 
public virtual Property Property {get;set;} // this what I need to choose. 
... 

プロパティを設定して、それが<property>remote</property>を設定し、<property>local</property>かのデータベースから同じ構造化とローカルコンテナからのファイルならば、データベースからプロパティテーブルから取得する必要があります。

class Property{ 
    public int PropertyId {get;set} 
    public string name {get;set;} 
    public virtual ICollection<MyModel> MyModels {get;set;} 
    public Property() 
    { 
     MyModels = new List<Model>(); 
    } 
} 

とローカルデータはこの場合には

List<Property> lProperty = new List<Property>() 
{{PropertyId = 1,name = "val1"}, 
{PropertyId = 2,name = "val2"}, 
{PropertyId = 3,name = "val3"} ...} 
+0

をあなたに同じ恐ろしいの書式を持っていますかコード? – abatishchev

答えて

1

のようなものです、あなたはEFの関係から拒否し、このような何か書く必要があります。

public class MyModel 
{ 
    public int MyModelId { get; set; } 

    //PropertyId now is not real FK to PropertyTable at DB it is just another column, 
    //but it still will store reference to Property 
    public int? PropertyId { get; set; } 

    [NotMapped] 
    public Property Property 
    { 
     get 
     { 
      if (PropertyId.HasValue) 
      { 
       if (ForExampleStaticClass.config("property") == "remote") 
        using (var context = new Context()) 
        { 
         return context.PropertyTable.Where(x => x.PropertyId == PropertyId.Value).FirstOrDefault(); 
        } 
       else 
        return ForExampleStaticClass.lProperty.Where(x => x.PropertyId == PropertyId.Value).FirstOrDefault(); 
      } 
      else 
       return null; 
     } 
     set 
     { 
      //I consider that Property(value) already exists at DB and/or Local Storage.       
      PropertyId = value.PropertyId; 
     } 
    } 
}