2017-06-12 1 views
0

Ich bin mit adrett und willDapper C#: Split Spalte Typ Objekt in TypeID und Type-Namen

Class EType 
{ 
    int TypeID; 
    string TypeName; 
} 

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

Mitarbeiter Tabelle aufgeteilt:

ID||Location||TypeID||TypeName 

Während CRUD-Operationen unter Verwendung von Dapper, ich will Umwandlung von empType in entsprechende Spalten in der DB automatisch.Tried Typ Handler und benutzerdefinierte Karten. .?. Aber kein Glück :(

Jede Hilfe, bitte

Antwort

0

Zuerst müssen Sie Eigenschaften verwenden, keine Felder Verwenden Sie dann die Dapper 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

Dank viel für die Hilfe Gibt es trotzdem. Auch das Einfügen und Aktualisieren von EType in Typeeid und Typ automatisch übernehmen –

+0

Nicht, dass ich weiß ... –