Ich bin neu in Automapper. Mit den unten stehenden Links versuche ich es in Aktion zu verstehen.Wie benutzt man die neueste Version von Automapper?
- http://automapper.org/
- https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper/
ich seine AutoMapper v verwende ist 5.2.0
Hier meine Sachen. https://codepaste.net/xph2oa
class Program
{
static void Main(string[] args)
{
//PLEASE IGNORE NAMING CONVENTIONS FOR NOW.Sorry!!
//on Startup
AppMapper mapperObj = new AppMapper();
mapperObj.Mapping();
DAL obj = new DAL();
var customer = obj.AddCustomers();
}
}
class Customer
{
public int CustomerId { get; set; }
public string CustName { get; set; }
}
class CustomerTO
{
public int CustId { get; set; }
public object CustData { get; set; }
}
class AppMapper
{
public void Mapping()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerTO>();
});
IMapper mapper = config.CreateMapper();
}
}
class DAL
{
public IEnumerable<CustomerTO> AddCustomers()
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer() { CustName = "Ram", CustomerId = 1 });
customers.Add(new Customer() { CustName = "Shyam", CustomerId = 2 });
customers.Add(new Customer() { CustName = "Mohan", CustomerId = 3 });
customers.Add(new Customer() { CustName = "Steve", CustomerId = 4 });
customers.Add(new Customer() { CustName = "John", CustomerId = 5 });
return customers; //throws error
}
}
Fehler -Cannot implizit Typ System.Collections.Generic.List‘zu 'System.Collections.Generic.IEnumerable' konvertieren. Eine explizite Konvertierung vorhanden ist (möglicherweise fehlt eine Guss?)
Wie ordne ich List<Customer>
-List<CustomerTO>
?
Bitte beachten Sie, dass in Customer
Ich habe Eigenschaft vom Typ string
mit Namen Custname
während CustomerTO
ich die Eigenschaft mit Namen CustData
vom Typ object
. Also, wie kann ich diese andere Namenseigenschaft zuordnen?
Danke.
Check [diese] (http://stackoverflow.com/questions/37348788/automapper-5-0-global-configuration) ich denke, es wird Ihnen helfen. Aber ich weiß nicht, ob du von "string" zu "object" "map" kannst. –
Hast du das Wiki angeschaut? Es hat die aktuellste Dokumentation anstelle meines Blogs, der veraltet sein kann (zum Beispiel ist die statische API immer noch dort und wird es sein). –
@JimmyBogard, Danke für den Blog. Dein Blog + einige andere Links waren ausreichend genug, um loszulegen. Ich habe das Wiki noch nicht überprüft. –