Reputation: 119
I have a reference file (dll) containing a class, which i use as my base class:
public class Group
{
public Group();
public int Id { get; set; }
public int League_Id { get; set; }
public string Name { get; set; }
public string Nationality { get; set; }
}
(Please note that the Group class contains about 30 entities, i have only displayed some)
Since i use Entity Framework the ID needs to be unique. i get this data from a API call and noticed that the ID is not unique, i have added my own class.
public class modGroup : APICall.Group
{
public modGroup()
{
modID = 0;
}
[Key]
public int modID { get; set; }
}
This setup works, EF is creating the database.
What i'd like to do is get the data from the API (Which is structured as the Group class), create a new modGroup() and set all data from the API call, without referencing each individual object.
What i would like to do is transfer data without setting each individual entity.
List<APICall.Group> groupData= _ApiRequester.GetGroup();
using (var a = new databaseModel())
{
foreach (APICall.Group x in groupData)
{
var modGroup = new Models.modGroup();
modGroup.modID = 0;
// fill modgroup with rest of variables without doing:
// modGroup.League_iD = a.League_iD;
// modGroup.Name = a.Name;
// etc
}
}
Upvotes: 1
Views: 103
Reputation: 3009
I would use Automapper to map the two classes together in one call. This type of situation is what it was designed for. All you would need to do is create a mapping configuration and then map the two classes. It's a very simple but powerful tool. e.g. Something like this:
var config = new MapperConfiguration(cfg => cfg.CreateMap<APICall.Group, modGroup>()
.ForMember(dest => dest.modID, s=>s.MapFrom(s=>s.Id));
// etc create more mappings here
);
var mapper = config.CreateMapper();
List<modGroup> modGroupList = mapper.Map<List<modGroup>>(groupData);
Upvotes: 2