automapper for ef6 list value - entity-framework

public class TestEntity
{
public string Name { get; set; }
}
public class Test
{
public string name { get; set; }
}
List<TestEntity> testEntities;
List<Test> test;
testEntities= _db.testEntity.ToList();
How do I use AutoMapper to map the value from testEntities to test using EF6?

In order to use Automapper you need to to install Automapper . The create a static class and call it at application start method . (its optional step but for maintenance purpose you should have a class which have all mapping defined and then call it globally and then you can use it .)
then
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.CreateMap<TestEntity, Test>().ReverseMap();
}
}
then
to use it
AutoMapper.Mapper.Map<TestEntity, Test>(entity);
for list use like this
AutoMapper.Mapper.Map<List<TestEntity>,List< Test>>(entity);
in case you have different name of properties then you can use ForMember function and map them individual .
for example
Mapper.CreateMap<TestEntity, Test>()
.ForMember(p => p.name, opt =>
opt.MapFrom(src => src.Name));
and wise versa .

This is from one of my working projects, maybe this can help you. I am using Automapper 5.2.0.0
The case below is just to map two different classes
var referenceInfo = service.GetReferenceNumber(criteria);
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<ReferenceInfo, ReferenceInfoModel>();
});
var mapper = config.CreateMapper();
var dest = mapper.Map<ReferenceInfo, ReferenceInfoModel>(referenceInfo);
if you want to create a map from a list or in my case IEnumerable it should be like
IEnumerable<ReferenceInfo> entityList = bizService.GetReferenceList();
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<ReferenceInfo, ReferenceInfoModel>();
});
var mapper = config.CreateMapper();
var dest = mapper.Map<IEnumerable<ReferenceInfo>, IEnumerable<ReferenceInfoModel>>(entityList);
Hope this helps

Related

Is it possible to access a shared TPH column in EF Core without using intermediate classes?

When using shared columns in an EF Core TPH setup, is it possible to access the shared column during projection?
class Program
{
public static readonly ILoggerFactory MyLoggerFactory
= LoggerFactory.Create(builder => {
builder.AddConsole();
});
static async Task Main(string[] args)
{
using (var context = new ClientContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var actions = await context.Actions
.Select(a => new
{
Id = a.Id,
// this works - but really messy and complex in real world code
Message = (a as ActionA).Message ?? (a as ActionB).Message,
// this throws "Either the query source is not an entity type, or the specified property does not exist on the entity type."
// is there any other way to access the shared column Message?
// Message = EF.Property<string>(a, "Message"),
})
.ToListAsync();
actions.ForEach(a => Console.WriteLine(a.Id + a.Message));
}
}
public class ActionBase
{
public int Id { get; set; }
// ... other shared properties
}
public class ActionA : ActionBase
{
// shared with B
[Required]
[Column("Message")]
public string Message { get; set; }
// ... other specific properties
}
public class ActionB : ActionBase
{
// shared with A
[Required]
[Column("Message")]
public string Message { get; set; }
// ... other specific properties
}
public class ActionC : ActionBase
{
public string SomethingElse { get; set; }
// ... other specific properties
}
class ClientContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// TO USE SQL
//optionsBuilder
// .UseLoggerFactory(MyLoggerFactory)
// .UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=TPHSharedColumn;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30")
// .EnableSensitiveDataLogging(false);
// TO USE INMEMORY
optionsBuilder
.UseLoggerFactory(MyLoggerFactory)
.UseInMemoryDatabase(Guid.NewGuid().ToString());
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ActionA>().HasData(new ActionA()
{
Id = 1,
Message = "A"
});
builder.Entity<ActionB>().HasData(new ActionB()
{
Id = 2,
Message = "B"
});
builder.Entity<ActionC>().HasData(new ActionC()
{
Id = 3,
SomethingElse = "C"
});
}
public DbSet<ActionBase> Actions { get; set; }
}
}
In this simple example, it would of course be possible to move Message to the base class - but that would make it possible to accidentally add an ActionC with a Message since I would need to remove the Required attribute.
I also know I could add a ActionWithRequiredMessage intermediate class to inherit ActionA and ActionB with, but again - in the much more complex real world example this is not feasible since there are also other shared columns and C# does not allow inheriting from multiple classes - and EF Core does not seem to like to use interfaces for this.
I simply would like to find a way to directly access the shared column - and use it in a projection.
Anyone know if this is possible?
I can't find it documented, but in EF Core 5.x you can access the shared column using any of the derived entities having a property mapped to it, e.g. all these work
Message = (a as ActionA).Message,
Message = (a as ActionB).Message,
Message = ((ActionA)a).Message,
Message = ((ActionB)a).Message,

Automapper configuration setup

I am using entity framework for my DAL and want to convert entities objects to business objects and vice versa. This is taking place in my BLL project. I am hoping to setup automapper in my BLL project to take... let say Customer.cs auto generated by EF and convert it to CustomerWithDifferentDetail.cs (my business obj)
I attempted to create an AutoMapperBLLConfig.cs under BLL project with the following code:
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile(new CustomerProfile());
});
}
public class CustomerProfile : Profile
{
protected override void Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerWithDifferentDetail>();
cfg.CreateMap<CustomerWithDifferentDetail, Customer>();
});
}
}
Then I created CustermerService.cs under BLL project with the following code to test if it's working:
public void CustomerToCustomerWithDifferentDetail()
{
AutoMapperBLLConfiguration.Configure();
Customer source = new Customer
{
Account = 1234,
Purchase_Quantity = 100,
Date = "05/05/2016",
Total = 500
};
Models.CustomerWithDifferentDetail testCustomerDTO = Mapper.Map<Customer, Models.CustomerWithDifferentDetail>(source)
}
I get this Error:
Missing type map configuration or unsupported mapping.
I am not sure what I did wrong. I don't have a start_up or global.aspx. This is a class library. I'm not sure what I'm missing or did wrong.
I have a separate project calls Models which hold all the business objects including CustomerWithDifferentDetail.cs. In this case, CustomerWithDifferentDetail only has two properties: Account and Total. If mapped, it should give me Account = 1234 and Total = 500 - basically the same data as entity object just in different shape.
======================= UPDATE=================================
AutoMapperBLLConfig.cs - stay the same as noted above
CustomerProfile.cs
public class CustomerProfile : Profile
{
protected override void Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerWithDifferentDetail>().ReverseMap(); //cut it down to one line with ReverseMap
});
}
CreateMap<Customer, CustomerWithDifferentDetail>().ReverseMap(); //missed this one line before; hence, the error
}
CustomerService.cs
static CustomerService()
{
AutoMapperBLLConfiguration.Configure(); //per #Erik Philips suggestion, move this call to a static constructor
}
public void CustomerToCustomerWithDifferentDetail()
{
Customer source = new Customer
{
Account = 1234,
Purchase_Quantity = 100,
Date = "05/05/2016",
Total = 500
};
Models.CustomerWithDifferentDetail testCustomerDTO = Mapper.Map<Customer, Models.CustomerWithDifferentDetail>(source);
}
Result: my testCustomerDTO returns exactly what I expected.
Since you are using the instance method of AutoMapper:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerWithDifferentDetail>();
cfg.CreateMap<CustomerWithDifferentDetail, Customer>();
});
Then you need to use the instance for mapping:
Models.CustomerWithDifferentDetail testCustomerDTO =
config.Map<Customer, Models.CustomerWithDifferentDetail>(source)
I personally haven't really thought this through in my applications (I need to move to the instance method instead of the static method). (Migrating from status API).
Off the cuff, based on your code, I'd probably do something like:
public class PersonDataObject
{
public string Name { get; set; }
}
public class PersonBusinessObject
{
private readonly MapperConfiguration _mapper;
public string Name { get; set; }
PersonBusinessObject()
{
_mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<PersonDataObject,PersonBusinessObject>();
});
}
public static PersonBusinessObject MapFrom(PersonDataObject data)
{
return _mapper.Map<PersonBusinessObject>(data);
}
}
Then you can simply:
PersonDataObject data = new PersonDataObject();
PersonBusinessObject business = PersonBusinessObject.MapFrom(data);

How to create custom entity for Web API 2 OData Data controller

I have the need to migrate my traditional Web API 2 Data controllers over to OData v4 style data controllers. This works very easily with the standard one-to-one table-to-entity relationship but I now have the need to use several different tables (which have no real constraints) into my data controller response. I am having trouble figuring out how to register this new custom "entity" in my WebAPI.config file.
Here is an example of what my WebAPIconfig.cs looks like:
using System.Linq;
using System.Web.Http;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;
using System.Web.OData.Routing.Conventions;
using MyProject.Models;
namespace MyProject
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and routes
config.MapHttpAttributeRoutes();
//OData configuration
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Order>("orders");
builder.EntitySet<Customer>("customers");
//what goes here for my "custom" entity?
var _model = builder.GetEdmModel();
var defaultConventions = ODataRoutingConventions.CreateDefaultWithAttributeRouting(config, _model);
//var defaultConventions = ODataRoutingConventions.CreateDefault();
var conventions = defaultConventions.Except(
defaultConventions.OfType<MetadataRoutingConvention>());
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "api",
routingConventions: conventions,
pathHandler: new DefaultODataPathHandler(),
model: _model);
//ensure JSON responses
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
}
Here is an example of what my Web API 2 data controller looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using MyProject.Models;
namespace MyProject.DataControllers
{
public class OrderDetailsController : ApiController
{
public OrderDetails GetOrderDetails(int id)
{
var ctx = new MyDatabaseEntities();
var details = new OrderDetails();
var order = ctx.Orders.FirstOrDefault(o => o.orderID == id);
if (order == null)
{
return details; //return an empty details object to the UI and abandon this code
}
//Data objects necessary for the order details page
IEnumerable<orderCertification> coupons = ctx.Coupons;
var importances = ctx.OrderImportances.Where(x => x.ImportanceId == order.ImportanceId).Where(x => (x.Type == "IMPORTANCES")) ?? null;
var rankings = ctx.OrderImportances.Where(x => x.ImportanceId == order.ImportanceId).Where(x => (x.Type == "RANK")) ?? null;
var profits = ctx.OrderImportances.Where(x => x.ImportanceId == order.ImportanceId).Where(x => (x.Type == "PROFIT")) ?? null;
var address = ctx.CustomerAddress.Where(c => c.OrderId == order.Id) ?? null;
var email = ctx.CustomerEmail.Where(c => c.Id == order.Id).Where(x => x.Type == "EMAIL") ?? null;
var giftcards = ctx.GiftCardAssignments.Where(c => c.CardID == order.CardID).FirstOrDefault().ToList() ?? null;
var customerCoupons = coupons.Where(c => giftCards.Any(o => o.GiftCardID == c.Id)).OrderBy(c => c.CouponName) ?? null;
//lots of other fun and crazy properties get set here!! etc etc.
//Set the order details properties
details.OrderImportances = importances;
details.OrderRankings = rankings;
details.OrderProfits = profits;
details.OrderAddress = address;
details.OrderEmail = email;
details.OrderGiftCards = giftcards;
details.OrderCoupons = customerCoupons;
details.OrderDescription = "This is my order description string.";
return details;
}
}
}
And here is an example of what my OrderDetails() class currently looks like:
using System.Collections.Generic;
namespace MyProject.Models
{
public class OrderDetails
{
public IEnumerable<OrderImportance> OrderImportances { get; set; }
public IEnumerable<OrderImportance> OrderRankings { get; set; }
public IEnumerable<OrderImportance> OrderProfits { get; set; }
public string OrderAddress { get; set; }
public string OrderEmail { get; set; }
public IEnumerable<OrderGiftCard> OrderGiftCards { get; set; }
public IEnumerable<OrderCoupon> OrderCoupons { get; set; }
public string OrderDescription { get; set; }
}
}
How do I make the OData version of this Web API controller and how do I register my OrderDetails class for it, in my WebAPIConfig.cs?
OrderDetails does not appear to have a key property. Therefore, it is not an entity type (a named structured type with a key), but rather a complex type (a keyless named structured type consisting of a set of properties).
Since complex types do not have their own identity (i.e., key), they cannot be exposed as entity sets in an OData service. This means you will not configure OrderDetails in the model builder, nor will you create a separate controller for OrderDetails.
The most straightforward way to migrate your existing GetOrderDetails method to OData is to redefine it as an OData function bound to the orders entity set. Actions and Functions in OData v4 Using ASP.NET Web API 2.2 provides a good tutorial on defining and configuring OData functions, but here's the gist of what you need to do.
Declare the function in WebApiConfig.Register:
builder.EntityType<Order>().Function("GetOrderDetails").Returns<OrderDetails>();
Define the function in the controller for the orders entity set:
public class OrdersController : ODataController
{
// Other methods for GET, POST, etc., go here.
[HttpGet]
public OrderDetails GetOrderDetails([FromODataUri] int key)
{
// Your application logic goes here.
}
}
Finally, invoke the function as follows:
GET http://host/api/orders(123)/Default.GetOrderDetails
Note that Default is the default namespace of the service, which is normally required when invoking a bound function. To change this, you can either set builder.Namespace, or you can enable unqualified function calls with config.EnableUnqualifiedNameCall(true).

Change name of Identity Column for all Entities

I am in the process of creating a domain model and would like to have a "BaseEntity" class with an "Id" property (and some other audit tracking stuff). The Id property is the primary key and each Entity in my Domain Model will inherit from the BaseEntity class. Pretty straightforward stuff.....
public class BaseEntity
{
[Key]
public int Id { get; set; }
public DateTime LastUpdate { get; set; }
public string LastUpdateBy { get; set; }
}
public class Location : BaseEntity
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
}
Using the example above, I would like to map the "Id" field to a "LocationId" column. I understand that I can use the modelBuilder to do this for each entity explicitly by doing something like this:
modelBuilder.Entity<Location>().Property(s => s.Id).HasColumnName("LocationId");
But I would like to do this for every Entity in my domain model and it would be ugly.
I tried the following bit of reflection but did not have any luck. For whatever reason, the compiler "cannot resolve symbol type":
foreach (var type in GetTypesInNamespace(Assembly.Load("Domain.Model"),"Domain.Model"))
{
modelBuilder.Entity<type>().Property(x=>x.Id).....
}
Is there a way to define a convention to override the default PrimaryKey convention to map my "Id" property to a "ClassNameId" property in the database? I am using Entity Framework 6.
You should take a look at Custom Code First Conventions. You need EF6 for it to work, but it looks like you're already using it.
Just to give you an overview, take a look at the following convention I've used to convert PascalCase names to underscore names. It includes a convention for id properties... It also includes an optional table name prefix.
public class UnderscoreNamingConvention : IConfigurationConvention<PropertyInfo, PrimitivePropertyConfiguration>,
IConfigurationConvention<Type, ModelConfiguration>
{
public UnderscoreNamingConvention()
{
IdFieldName = "Id";
}
public string TableNamePrefix { get; set; }
public string IdFieldName { get; set; }
public void Apply(PropertyInfo propertyInfo, Func<PrimitivePropertyConfiguration> configuration)
{
var columnName = propertyInfo.Name;
if (propertyInfo.Name == IdFieldName)
columnName = propertyInfo.ReflectedType.Name + IdFieldName;
configuration().ColumnName = ToUnderscore(columnName);
}
public void Apply(Type type, Func<ModelConfiguration> configuration)
{
var entityTypeConfiguration = configuration().Entity(type);
if (entityTypeConfiguration.IsTableNameConfigured) return;
var tableName = ToUnderscore(type.Name);
if (!string.IsNullOrEmpty(TableNamePrefix))
{
tableName = string.Format("{0}_{1}", TableNamePrefix, tableName);
}
entityTypeConfiguration.ToTable(tableName);
}
public static string ToUnderscore(string value)
{
return Regex.Replace(value, "(\\B[A-Z])", "_$1").ToLowerInvariant();
}
}
You use it like this
modelBuilder.Conventions.Add(new UnderscoreNamingConvention { TableNamePrefix = "app" });
EDIT: In your case, the Apply method should be something like this:
public void Apply(PropertyInfo propertyInfo, Func<PrimitivePropertyConfiguration> configuration)
{
if (propertyInfo.Name == "Id")
{
configuration().ColumnName = propertyInfo.ReflectedType.Name + "Id";
}
}
Try this out in your DbContext class;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<int>()
.Where(p => p.Name.Equals("Id"))
.Configure(c => c.HasColumnName(c.ClrPropertyInfo.ReflectedType.Name + "Id"));
}
int is the CLR Type of my Primary Key fields. I want to refer to all keys in code as Id but DBA's require keys to be Id with Table entity name prefix. Above gives me exactly what I want in my created database.
Entity Framework 6.x is required.
In Entity Framework 6 Code First:
modelBuilder.Entity<roles>().Property(b => b.id).HasColumnName("role_id");
and update-database...
Change in model
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long id { get; set; }
to:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long role_id { get; set; }
Then remove this:
//modelBuilder.Entity<roles>().Property(b => b.id).HasColumnName("role_id");
A start to the Dynamic approach if NOT using custom conventions
modelBuilder.Entity<Location>().Property(s => s.Id).HasColumnName("LocationId");
You can do this using reflection on the context. Pseudo Code as explanation:
Reflect Context to get a list of POCO names
For each POCO in a dbcontext.
Map Property Id -> string PocoName+Id
Here are the extensions I use for this type of solution.
// DBSet Types is the Generic Types POCO name used for a DBSet
public static List<string> GetModelTypes(this DbContext context) {
var propList = context.GetType().GetProperties();
return GetDbSetTypes(propList);
}
// DBSet Types POCO types as IEnumerable List
public static IEnumerable<Type> GetDbSetPropertyList<T>() where T : DbContext {
return typeof (T).GetProperties().Where(p => p.PropertyType.GetTypeInfo()
.Name.StartsWith("DbSet"))
.Select(propertyInfo => propertyInfo.PropertyType.GetGenericArguments()[0]).ToList();
}
private static List<string> GetDbSetTypes(IEnumerable<PropertyInfo> propList) {
var modelTypeNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
.Select(p => p.PropertyType.GenericTypeArguments[0].Name)
.ToList();
return modelTypeNames;
}
private static List<string> GetDbSetNames(IEnumerable<PropertyInfo> propList) {
var modelNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet"))
.Select(p => p.Name)
.ToList();
return modelNames;
}
However, you will still need to employee dynamic lambda to finish.
Continue that topic here: Dynamic lambda example with EF scenario
EDIT:
Add link to another question that address the common BAse Config class approach
Abstract domain model base class when using EntityTypeConfiguration<T>
Piggybacking on #Monty0018 's answer but this just need to be updated a little if, like me, you're using Entity Framework 7 and/or SQLite.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
try
{
_builder = modelBuilder;
var typeName = typeof(T).Name;
_builder
.Entity(typeof(T))
.Property<int>("Id")
.ForSqliteHasColumnName(typeName + "Id");
}
catch (Exception e)
{
throw e;
}
}

Decoupling Entity Framework from my POCO classes

I'm dynamically creating my DbContext by iterating over any entities that inherit from EntityBase and adding them to my Context:
private void AddEntities(DbModelBuilder modelBuilder)
{
var entityMethod = typeof(DbModelBuilder).GetMethod("Entity");
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var entityTypes = assembly.GetTypes()
.Where(x => x.IsSubclassOf(typeof(EntityBase)) && !x.IsAbstract);
foreach (var type in entityTypes)
{
dynamic entityConfiguration = entityMethod.MakeGenericMethod(type).Invoke(modelBuilder, new object[] { });
EntityBase entity = (EntityBase)Activator.CreateInstance(type);
//Add any specific mappings that this class has defined
entity.OnModelCreating(entityConfiguration);
}
}
}
That way, I can have many namespaces but just one generic repository in my base namespace that's used everywhere. Also, in apps that make use of multiple namespaces, the base repository will already be setup to use all the entities in all the loaded namespaces. My problem is, I don't want to make EntityFramework.dll a dependency of every namespace in the company. So I'm calling OnModelCreating and passing the EntityTypeConfiguration to the class so it can add any mappings. This works fine and here's how I can add a mapping to tell the model that my "Description" property comes from a column called "Descriptor":
class Widget... {
public override void OnModelCreating(dynamic entity)
{
System.Linq.Expressions.Expression<Func<Widget, string>> tmp =
x => x.Description;
entity.Property(tmp).HasColumnName("Descriptor");
}
The good thing is, my entity class has no reference to EF, this method is only called once, when the context is created and if we scrap EF and go to something else in the future, my classes won't have all sorts of attributes specific to EF in them.
The problem is, it's super ugly. How can I let the model know about column mappings and keys in a simpler way than creating these Expressions to get properties to map without hard coding references to EF all over my poco classes?
You could define your own Attributes and use these to control the configuration within OnModelCreating(). You should be able to gain (using reflection) all the details you need for column mapping in one linq query a second query for the creation of the key.
public class DatabaseNameAttribute : Attribute
{
private readonly string _name;
public DatabaseNameAttribute(string name)
{
_name = name;
}
public string Name
{
get
{
return _name;
}
}
}
public class KeySequenceAttribute : Attribute
{
private readonly int _sequence;
public KeySequenceAttribute(int sequence)
{
_sequence = sequence;
}
public int Sequence
{
get
{
return _sequence;
}
}
}
[DatabaseName("BlogEntry")]
public class Post
{
[DatabaseName("BlogId")]
[KeySequence(1)]
public int id { get; set; }
[DatabaseName("Description")]
public string text { get; set; }
}