2015-12-01 143 views
1

我的場景: 我有一個名爲Person的類,我使用Dapper保存到數據庫。 在Person上,我有一個字典值,我序列化爲一個字符串,並將其存儲爲varchar(MAX)。如何在將一種類型映射到另一種類型時使用Dapper的多映射?

public class Person 
{ 
    public string Name {get; set;} 
    public int Id {get; set;} //PK in DB 
    public int Age {get; set;} 
    public IDictionary<string, string> Values {get; set;} 
} 

這是我如何保存到數據庫:

DynamicProperties dp = new DynamicProperties(); 
dp.Add("Name", p.Name); 
dp.Add("Age", p.Age); 
dp.Add("Values", Jil.JSON.Serialize<IDictionary<string, string>>(p.Values)); 

conneciton.Execute(insertSql, dp, commandType: CommandType.StoredProcedure); 

這是我嘗試讀出來

private Func<Person, object, Person> GetPerson = new Func<Person, object, Person> 
((person, values) => { 
       person.Values = Jil.JSON.Deserialize<Dictionary<string, string>>((string)values); 
       return person; 
      }); 

string sql = "SELECT text 
       FROM otherTable 

       SELECT Name, Id, Age, Values 
       FROM People 
       WHERE Id = @Id" 

SqlMapper.GridReader gridReader = connToDeviceConfig.QueryMultiple(sql, new {Id = 5}, commandType: CommandType.StoredProcedure); 

List<string> listOfOtherStuff = gridReader.Read<string>().ToList(); 
List<Person> people = gridReader.Read<Person, object, Person>(GetPerson, splitOn: "Age").ToList(); 

// listOfOtherStuff和人民獨立

Th E二次gridReader與失敗當使用多地圖API確保您設置的splitOn PARAM如果你有多個id其他鍵\ r \ n參數名:splitOn

我覺得我也許彎曲小巧玲瓏的一點嘗試並讓它做一些不應該做的事情,例如從 中讀取一個字符串,並將其反序列化爲字典並賦值給Person.Values。

這是這樣做的(我只是有一個bug的地方)?或者我應該採取另一種方法?

我用這作爲ref: (Link to approximate location in file at Archive.org)

public void TestProcSupport() 
{ 
    var p = new DynamicParameters(); 
    p.Add("a", 11); 
    p.Add("b", dbType: DbType.Int32, direction: ParameterDirection.Output); 
    p.Add("c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); 

    connection.Execute(@"create proC#TestProc 
@a int, 
@b int output 
as 
begin 
set @b = 999 
select 1111 
return @a 
end"); 
    connection.Query<int>("#TestProc", p, commandType: CommandType.StoredProcedure).First().IsEqualTo(1111); 

    p.Get<int>("c").IsEqualTo(11); 
    p.Get<int>("b").IsEqualTo(999); 

} 

堆棧跟蹤:

at Dapper.SqlMapper.GetNextSplit(Int32 startIdx, String splitOn, IDataReader reader) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 2111 
    at Dapper.SqlMapper.GenerateDeserializers(Type[] types, String splitOn, IDataReader reader) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 2057 
    at Dapper.SqlMapper.<MultiMapImpl>d__71`8.MoveNext() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1857 
    at Dapper.SqlMapper.GridReader.<MultiReadInternal>d__9`8.MoveNext() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4300 
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
    at Dapper.SqlMapper.GridReader.Read[TFirst,TSecond,TReturn](Func`3 func, String splitOn, Boolean buffered) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 4330 
+0

修正了一些我認爲可能會讓人誤以爲是試圖閱讀這些內容的錯誤。 – scheepersw

回答

0

試過其他splitOn S'

我沒有一個方便的數據庫測試,但我認爲splitOn應該是下一個映射對象中的第一列,而不是之前的最後一列。此外,列成爲對象的成員,而不是整個值(儘管可能會出現單列情況的例外情況,同樣我也沒有方便的測試數據庫)。

TL; DR - 嘗試這樣的事情

class ValuesObj{ 
    public string Values {get;set;} 
} 

// whatever code here 

private Func<Position, ValuesObj, Position> GetPerson = new Func<Person, ValuesObj, Person> 
((person, valuesObj) => { 
       string values = valuesObj.Values; 
       person.Values = Jil.JSON.Deserialize<Dictionary<string, string>>((string)values); 
       return position; 
      }); 

string sql = "SELECT text 
       FROM otherTable 

       SELECT Name, Id, Age, Values 
       FROM People 
       WHERE Id = @Id" 

SqlMapper.GridReader gridReader = connToDeviceConfig.QueryMultiple(sql, new {Id = 5}, commandType: CommandType.StoredProcedure); 

List<string> listOfOtherStuff = gridReader.Read<string>().ToList(); 
List<Person> people = gridReader.Read<Person, ValuesObj, Person>(GetPerson, splitOn: "Values").ToList(); 

否則,我只是用dynamic回報小巧玲瓏的方法建議。

+0

非常感謝。那就是訣竅。 – scheepersw

相關問題