簡體   English   中英

按屬性名稱動態訪問 Class (dbcontext) 屬性

[英]Access a Class (dbcontext) Property Dynamically by Property Name

我有一個 OnPost 方法,專門用於更新名為 Status 的特定 object:

public async Task OnPostStatus()
{
            Status ExistingRecord = await _context.Status.FirstOrDefaultAsync(m => m.Id == Status.Id);

            if (ExistingRecord == null)
            {
                _context.Status.Add(Status);

            } else
            {
                ExistingRecord.Description = Status.Description;
                ExistingRecord.Hint = Status.Hint;
                _context.Attach(ExistingRecord).State = EntityState.Modified;
            }
            await _context.SaveChangesAsync();
            StatusMessage = "Saved!";
            await OnGetAsync();

        }

但我實際上對其他 4 個具有完全相同屬性的對象執行了完全相同的 OnPost。 因此,我想通過將 object 的類型傳入 OnPost 來使其更具動態性,如下所示:

        public async Task OnPost(string objectName)
        {
            var objectName ExistingRecord = await _context.(objectName).FirstOrDefaultAsync(m => m.Id == (objectName).Id);

            if (ExistingRecord == null)
            {
                _context.(objectName).Add((objectName));

            } else
            {
                ExistingRecord.Description = (objectName).Description;
                ExistingRecord.Hint = (objectName).Hint;
                _context.Attach(ExistingRecord).State = EntityState.Modified;
            }
            await _context.SaveChangesAsync();
            StatusMessage = "Saved!";
            await OnGetAsync();

        }

我不知道具體的語法!

由於這些類具有完全相同的字段,您可以創建a common class作為基礎 class 來存儲字段。

在我的示例中,有三個表(tb1、tb2、tb3)和一個基礎 class( tbBase ),基礎 class 與數據庫無關:

 public class tbBase
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string Hint { get; set; }
}
public class tb1: tbBase
{

}
public class tb2 : tbBase
{

}
public class tb3 : tbBase
{

}

然后從視圖上看,將tbBase class as the parameter傳遞給post方法,以及需要修改a specific table type parameter

<form method="post">

    Id:<input id="Id" type="text" name="Id" /><br/>
    Description: <input id="Description" type="text" name="Description" /><br/>
    Hint: <input id="Hint" type="text" name="Hint" /><br/>
    table type:<input id="Text1" type="text" name="type" />
    <input id="Button1" type="submit" value="button" />
</form>

在 PageModel 中,通過Dictionary存儲參數表類型和具體類型的鍵值對:

        public async Task OnPost(tbBase table, string type)
    {
        Dictionary<string, Type> TableTypeDictionary = new Dictionary<string, Type>()
    {
          { "tb1", typeof(tb1) },
          { "tb2", typeof(tb2) },
          { "tb3", typeof(tb3) }
    };
        dynamic obj = Activator.CreateInstance(TableTypeDictionary[type]);
        for (int i = 0; i < obj.GetType().GetProperties().Length; i++)
        {
            obj.GetType().GetProperties()[i].SetValue(obj, table.GetType().GetProperty(obj.GetType().GetProperties()[i].Name).GetValue(table, null), null);
        }
        var ExistingRecord = _context.Find(TableTypeDictionary[type], obj.Id);
        if (ExistingRecord == null)
        {
            obj.Id = 0;//ID is self growing, no need to customize settings
            _context.Add(obj);
        }
        else
        {
            ExistingRecord.Description = table.Description;
            ExistingRecord.Hint = table.Hint;
            _context.Attach(ExistingRecord).State = EntityState.Modified;
        }
        await _context.SaveChangesAsync();
        StatusMessage = "Saved!";
        await OnGetAsync();

    }

暫無
暫無

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

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