亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動態查找 CRUD 操作的實際 DbSet

動態查找 CRUD 操作的實際 DbSet

C#
紅顏莎娜 2022-06-12 10:42:18
我在此站點上進行了搜索,但無法從我的上下文中獲取實際的 DbSet。我正在嘗試根據表名動態檢索每個數據庫集。 var dynamicdbset = GetDbSetByTableName(uploadTableName); //Dbset name is Mytables  private dynamic GetDbSetByTableName(string tableName)        {            MyEntities context = new MyEntities();            System.Reflection.PropertyInfo[] properties = typeof(ClearGUIEntities).GetProperties();            var prop = properties.FirstOrDefault(p => p.Name == tableName + "s");            using (var db = new MyEntities())            {                var dbset = prop?.GetValue(db);                return dbset;            }        }這里的問題是它返回了一些通用數據庫集,但我不能使用 linq 也不能做一個簡單的操作,比如dynamicdbset.Where(t = > t.Id == 123).Single();我需要能夠通過 tablename 動態獲取 dbset,并且還需要以與創建數據相同的方式查詢數據var value = context.MyTables.FirstorDefault()
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

返回的動態DbSet實際上只是真實DbSet對象的包裝器,您可以簡單地將其轉換為。然而,問題是,如果不使用泛型方法DbSet,就無法推斷出的類型。


以下方法可行,但可能是最不可取的:


private IEnumerable<T> GetDbSetByTableName<T>(string tableName)

{

    System.Reflection.PropertyInfo[] properties = typeof(ClearGUIEntities).GetProperties();

    var prop = properties.FirstOrDefault(p => p.Name == tableName + "s");


    using (var db = new ClearGUIEntities())

    {

        var dbset = prop?.GetValue(db);


        return new List<T>(dbset as IEnumerable<T>);

    }

}

現在,要解決這個問題,我們至少有兩個選擇:

  1. DbSet創建一個由所有s實現的接口(具有您需要的所有基本屬性) 。這樣,我們可以轉換動態對象,而無需在轉換時指定類型。

  2. 返回一個IEnumerable<dynamic>可以即時施放的。

選項1

public interface IBaseProperties

{

    int Id { get; set; }

    string Name { get; set; }

}


public class MyTable : IBaseProperties

{

    // Add these with either T4 templates or create partial class for each of these entities

    public int Id { get; set; }

    public string Name { get; set; }

}


private IEnumerable<IBaseProperties> GetDbSetByTableName(string tableName)

{

    System.Reflection.PropertyInfo[] properties = typeof(ClearGUIEntities).GetProperties();

    var prop = properties.FirstOrDefault(p => p.Name == tableName + "s");


    using (var db = new ClearGUIEntities())

    {

        var dbset = prop?.GetValue(db);


        return new List<IBaseProperties>(dbset as IEnumerable<IBaseProperties>);

    }

}


// ...

// Using it

// ...


var dynamicdbset = GetDbSetByTableName("MyTable");


int id = dynamicdbset.FirstOrDefault().Id;

選項 2

private IEnumerable<dynamic> GetDbSetByTableName(string tableName)

{

    System.Reflection.PropertyInfo[] properties = typeof(ClearGUIEntities).GetProperties();

    var prop = properties.FirstOrDefault(p => p.Name == tableName + "s");


    using (var db = new ClearGUIEntities())

    {

        var dbset = prop?.GetValue(db);


        return new List<dynamic>(dbset as IEnumerable<dynamic>);

    }

}


// ...


// At this point, you can basically access any property of this entity

// at the cost of type-safety

string id = dynamicdbset.FirstOrDefault().Id;

string name = dynamicdbset.FirstOrDefault().Name;

順便說一句,強制轉換List<T>是必要的,因為您使用的是using塊之外的對象,此時它將被處置。


new List<IBaseProperties>(dbset as IEnumerable<IBaseProperties>);


查看完整回答
反對 回復 2022-06-12
  • 1 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號