2017-06-12 66 views
0

我使用短小精悍,並要分割小巧玲瓏的C#:拆分列類型對象爲TYPEID和類型名

Class EType 
{ 
    int TypeID; 
    string TypeName; 
} 

Class Employee 
{ 
    int id; 
    string location; 
    EType EmpType; 
} 

Employee表:

ID||Location||TypeID||TypeName 

在CRUD操作使用小巧精緻,我想的empType的轉換自動分爲數據庫中的相應列。引用類型處理程序和自定義地圖。 ?但沒有運氣:(

任何幫助,請

回答

0

首先,你必須使用屬性,而不是字段然後用精緻小巧的multi-mapping

public class Employee 
{ 
    public int Id { get; set; } 
    public string Location { get; set; } 
    public EType EType { get; set; } 
} 

public class EType 
{ 
    public int TypeId { get; set; } 
    public string TypeName { get; set; } 
} 

[TestFixture] 
public class MultimappingTest 
{ 
    [Test] 
    public void TestSplit() 
    { 
     using (var conn = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=foo")) 
     { 
      var result = 
       conn.Query<Employee, EType, Employee>(@"select Id = 1, Location = 'earth', TypeId = 2, TypeName = 'human'", 
        (employee, type) => 
        { 
         employee.EType = type; 
         return employee; 
        }, splitOn: "TypeId").First(); 

      Assert.That(result.EType.TypeId == 2); 
      Assert.That(result.EType.TypeName == "human"); 
     } 
    } 
} 
+0

非常感謝您的幫助反正是有。來處理插入和更新?將EType自動分成Typeid和Type名稱 –

+0

不是我知道的... –