2016-11-08 85 views
0

this post中,我詢問並回答瞭如何動態地導入EF/DbSet模型中不帶硬編碼的類&屬性的值。我簡直無法相信自己徘徊了很長時間才找到了一個辦法。但是......如何動態地將繼承的屬性添加到EF數據存儲區?

據透露,通過本地窗口的實例層次看小時解決不了一個問題:

雖然我可以在目標類動態導入數據,從基類的任何屬性它從失敗延伸。上面的最初的帖子最後說明了這一點。在將數據導入DataTable後,我們的問題就會顯示出來。我們正在瀏覽DataTable的列,使用數據庫中的匹配類型來確定它們的類型(int,double等)。

要做到這一點,只要我們從Person()(基類)中選擇一個,就會創建一個對象,我們從中查詢信息,而對於Child一個空引用。

,而無需重新發帖稱整個文章中,我將只粘貼相關位:

foreach (DataRow dr in dt.Rows) 
{ 
    i = 0; // I don't like to put var instantiation in a loop... 
    // each drItem is the content for the row (theObj) 
    foreach (string drItem in dr.ItemArray) 
    { 
     string entAttrName = dt.Columns[i].ToString(); 
     string entAttrValue = dr[i].ToString(); 
     // column (property) name: 
     // the value of that property to load into this class' property 
     // which type of data is this property? (string, int32, double...) 
     // -also has data like if nullable, etc. of use in later refinements... 
     TypeInfo thisTypeInfo = theObj.GetType().GetTypeInfo(); 
     // All details from the property to update/set: 

>>---> PropertyInfo theProp = thisTypeInfo.GetDeclaredProperty(entAttrName); 

上面,最後一行無法分配theProp有效的對象,而不是交給一個null,這barfs我們的計劃,只要它查詢它。

上面鏈接中的示例(此代碼片段來自此處)正常工作只要您只能從Child()類中導入值。繼承的屬性導致它停在上面的行。

entAttrName僅僅是屬性的名稱,從早期的DataTable的標題行所採取的字符串,(滑過去的上面幾行代碼,而),本質上是做類似:

var aClass = u.CreateInstanceOf(dt.TableName, pathToAssembly); 
DbSet dbs = ctx.Set(aClass.GetType()); 
var theObj = dbs.Create(aClass.GetType()); 
foreach (DataRow dr in dt.Rows)... 
foreach (string drItem in dr.ItemArray)... 
string entAttrName = dt.Columns[i].ToString(); 
string entAttrValue = dr[i].ToString(); 
TypeInfo thisTypeInfo = theObj.GetType().GetTypeInfo(); 
PropertyInfo theProp = thisTypeInfo.GetDeclaredProperty(entAttrName); ****** 
if (theProp.PropertyType.ToString() == "System.String") 
{ 
    theProp.SetValue(theObj, (String)entAttrValue); 
} 
else if (theProp.PropertyType.ToString().Contains("System.Int32")) 
{ 
    theProp.SetValue(theObj, int.Parse(entAttrValue)); 
} else if... 

我不能弄清楚我爲什麼兒童()的生活......

class Child : Person 
{ 
    [Key] 
    [Column(Order = 0)] 
    public int Id { get; set; } 
    public string sChildFoo { get; set; } 
    public int iChildBar { get; set; } 
    public double dChildBaz { get; set; } 
} 

...性能順利通過該線,但任何人從()的屬性...

public abstract class Person 
{ 
    [Key] 
    public int PersonId { get; set; } 
    public int PersonAge { get; set; } 
    public int PersonWeight { get; set; } 
    public string PersonName { get; set; } 
} 

...失敗。這是程序的完整日誌,顯示它停止的地方。唯一的例外是在快照原帖:

2016-11-08 15:03:12.9049 INFO Starting at 3:03:12 PM 
2016-11-08 15:03:12.9801 INFO Created .Name: Qeququ Qequququ 
2016-11-08 15:03:12.9838 INFO Created .sParentFoo: Kakikikiki 
2016-11-08 15:03:13.9918 INFO wb.WorkSheets count: 2 
2016-11-08 15:03:14.0007 INFO ws.Rows count: 3 
2016-11-08 15:03:14.0007 INFO dt.Rows.Count: 2 
2016-11-08 15:03:14.0007 INFO dt.Name: Child 
2016-11-08 15:03:14.0666 INFO aClass.FullName: DynamicEFLoading.Child 
2016-11-08 15:03:14.0666 INFO Creating 'dbs' object... 
2016-11-08 15:03:14.0891 INFO GetType: DynamicEFLoading.Child 
2016-11-08 15:03:14.0963 INFO ================= row ================================== 
2016-11-08 15:03:14.0963 INFO ================= col 0 
2016-11-08 15:03:14.1105 INFO [0] Item: sChildFoo 
2016-11-08 15:03:14.1105 INFO [0] Value: Norwich 
2016-11-08 15:03:14.1105 INFO theProp.DeclaringType.FullName of attr: DynamicEFLoading.Child 
2016-11-08 15:03:14.1265 INFO theProp.GetSetMethod(true).ToString() of attr: Void set_sChildFoo(System.String) 
2016-11-08 15:03:14.1265 INFO theProp.GetType().ToString() of attr: System.Reflection.RuntimePropertyInfo 
2016-11-08 15:03:14.1265 INFO theProp.Name of attr: sChildFoo 
2016-11-08 15:03:14.1424 INFO theProp.PropertyType.ToString() of attr: System.String 
2016-11-08 15:03:14.1424 INFO theProp.ReflectedType.ToString() of attr: DynamicEFLoading.Child 
2016-11-08 15:03:14.1424 INFO theProp.ReflectedType.ToString() of attr: System.Void 
2016-11-08 15:03:14.1557 DEBUG Set System.String value: Norwich 
2016-11-08 15:03:14.1557 INFO ================= col 1 
2016-11-08 15:03:14.1557 INFO [1] Item: iChildBar 
2016-11-08 15:03:14.1780 INFO [1] Value: 29884 
2016-11-08 15:03:14.1780 INFO theProp.DeclaringType.FullName of attr: DynamicEFLoading.Child 
2016-11-08 15:03:14.1919 INFO theProp.GetSetMethod(true).ToString() of attr: Void set_iChildBar(Int32) 
2016-11-08 15:03:14.2113 INFO theProp.GetType().ToString() of attr: System.Reflection.RuntimePropertyInfo 
2016-11-08 15:03:14.2113 INFO theProp.Name of attr: iChildBar 
2016-11-08 15:03:14.2233 INFO theProp.PropertyType.ToString() of attr: System.Int32 
2016-11-08 15:03:14.2368 INFO theProp.ReflectedType.ToString() of attr: DynamicEFLoading.Child 
2016-11-08 15:03:14.2368 INFO theProp.ReflectedType.ToString() of attr: System.Void 
2016-11-08 15:03:14.2607 DEBUG Set System.Int32 value: 29884 
2016-11-08 15:03:14.2657 INFO ================= col 2 
2016-11-08 15:03:14.2851 INFO [2] Item: dChildBaz 
2016-11-08 15:03:14.2978 INFO [2] Value: 1.2 
2016-11-08 15:03:14.3184 INFO theProp.DeclaringType.FullName of attr: DynamicEFLoading.Child 
2016-11-08 15:03:14.3305 INFO theProp.GetSetMethod(true).ToString() of attr: Void set_dChildBaz(Double) 
2016-11-08 15:03:14.3305 INFO theProp.GetType().ToString() of attr: System.Reflection.RuntimePropertyInfo 
2016-11-08 15:03:14.3457 INFO theProp.Name of attr: dChildBaz 
2016-11-08 15:03:14.3682 INFO theProp.PropertyType.ToString() of attr: System.Double 
2016-11-08 15:03:14.3910 INFO theProp.ReflectedType.ToString() of attr: DynamicEFLoading.Child 
2016-11-08 15:03:14.4098 INFO theProp.ReflectedType.ToString() of attr: System.Void 
2016-11-08 15:03:14.4098 DEBUG Set System.Double value: 1.2 
2016-11-08 15:03:14.4098 INFO ================= col 3 
2016-11-08 15:03:14.4382 INFO [3] Item: PersonAge 
2016-11-08 15:03:14.4609 INFO [3] Value: 34 

對於一個完整,工作示例,這個故障看到the original post here

============================================== =============================

+1

'thisTypeInfo.GetDeclaredProperty'獲取由類型本身聲明的屬性,而不是它的超類型。使用'thisTypeInfo.GetProperty'。 –

+0

@GertArnold ---你做到了!這解決了繼承屬性的問題!我很認真,當我說我不知道​​我是如何做到的。它現在貫穿所有屬性!你應該得到答案!你會發布答案嗎?你可以粘貼這個:'/ /屬性的所有細節更新/設置: // PropertyInfo theProp = thisTypeInfo.GetDeclaredProperty(entAttrName); PropertyInfo theProp = thisTypeInfo.GetProperty(entAttrName);'(好吧,那根本沒用......) –

回答

1

thisTypeInfo.GetDeclaredProperty獲取屬性聲明的類型本身,而不是它的超類型。使用thisTypeInfo.GetProperty。或者只是

theObj.GetType().GetProperty(entAttrName); 
+0

再次謝謝你! –

相關問題