簡體   English   中英

將Entity Framework與“動態”實體一起使用?

[英]Using Entity Framework with “dynamic” entity?

例如,我有3個實體:產品,工具,設備。 它們都具有“id”和“Name”屬性。 現在我嘗試編寫一個方法來將實體的數據填充到組合框中。 但我現在不知道如何創建一個可以將任何實體傳遞給它的方法。 類似下面的代碼:

    private comboBox FillCombobox(List<EntityType>)
    {
        using (dowacodbEntities dowacodbEntities = new dowacodbEntities())
        {
            comboBox comboBox = new comboBox();
            List<EntityType> EntityList = dowacodbEntities.EntityType.ToList();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("Name");
            dt.Rows.Add(-1, "Choose one option");
            foreach (EntityType Entity in EntityList)
            {
                dt.Rows.Add(Entity.id, Entity.Name);
            }
            comboBox.ValueMember = dt.Columns[0].ColumnName;
            comboBox.DisplayMember = dt.Columns[1].ColumnName;
            comboBox.DataSource = dt;
            return combobox;
        }
    }

哪個EntityType可以是產品工具設備

我對我們使用的ASP.NET頁面有一個類似的基本實體控件,我們遵循Nathan Quirynen的建議。 在您的情況下,有一個基本實體控件,它傳遞給子類的屬性ID和Name。 通過這種控制,我們加載相同的下拉,然后用於訂單,設備和所有類型的其他實體。

public interface EntityLoader<T> where T : CommonEntity
{
  /*Load all the entities for the list*/
  List<T> LoadEntities();

  /*Load the single selected entity by its ID*/
  T LoadSingleEntity(String ID)
}

/*Common Entity to inherit*/
public class CommonEntity
{
  private String _entityID;
  private String _entityName;

  /*Unique ID of the entity*/
  public String ID 
  {
    get { return(_entityID);}
    set { _entityID = value;}
  }

  /*Entity Name*/
  public String Name
  {
    get { return(_entityName); }
    set { _entityName = value; }
  }
}

public EntityComboBox<T> where T : CommonEntity
{
    private comboBox FillCombobox(EntityLoader<T> dataLayer)
    {
            comboBox comboBox = new comboBox();
            List<EntityType> EntityList = dataLayer.LoadEntities();
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("Name");
            dt.Rows.Add(-1, "Choose one option");
            foreach (EntityType Entity in EntityList)
            {
                dt.Rows.Add(Entity.ID, Entity.Name);
            }
            comboBox.ValueMember = dt.Columns[0].ColumnName;
            comboBox.DisplayMember = dt.Columns[1].ColumnName;
            comboBox.DataSource = dt;
            return combobox;
    }
}

定義界面:

public interface IBaseEntity
{
    int Id { get; }
    string Name { get; }
}

在您的ProductToolDevice類中實現該接口(如果實體是自動生成的,則可以通過部分部分實現)並將您的方法定義為:

private ... FillCombobox<T>(...) where T : class, IBaseEntity
{
    using (var context = new Context())
    {
        var entities = context.CreateObjectSet<T>().ToList();
        ...
        foreach (IBaseEntity entity in entities)
        {
            dt.Rows.Add(entity.Id, entity.Name);
        }
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM