2017-06-12 71 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

不是我知道的... –