Let's say I have the following model:
class Foo
{
public delegate void Bar();
public Bar MyBar { get; set; }
}
And I would like to map this to my entity:
class FooEntity
{
public delegate void Bar();
public Bar MyBar { get; set; }
}
How is this done? I have tried using Automapper:
void ConfigureMappings()
{
// how else do I need to configure this?
Mapper.CreateMap<Foo, FooEntity>();
Mapper.CreateMap<Foo.Bar, FooEntity.Bar>(); // is this right?
}
It is important for me to have data drive this delegate, since there are hundreds of different methods I need to be able to switch between depending on the value of MyBar
. Any thoughts?
Edit: OK, so this no longer throws an exception when mapping from model to entity after moving the delegate out of the scope of each class:
public delegate void Bar(object obj);
public class Foo
{
public Bar MyBar { get; set; }
}
public class FooEntity
{
public Bar MyBar { get; set; }
}
Now I receive this: error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties FooEntity.MyBar in Set Foos. An Entity with Key (PK) will not round-trip when: Entity is type [Project.MyDbContext.FooEntity].
My database context code looks like this:
class MyDbContext : DbContext
{
DbSet<FooEntity> Foos { get; set; }
}
And I save this to the database:
public static class Test()
{
var foo = new Foo { MyBar = MyFancyDelegate };
using (var context = new MyDbContext())
{
context.Foos.Add(Mapper.Map<FooEntity>(foo));
context.SaveChanges();
}
}
public static void MyFancyDelegate(object obj)
{
Console.Write("test");
}
Is my problem just in what I am trying to do? Is it not possible to save delegate methods to a SQL table?
Bar
and serialize this into your SQL table ;) – Random Dev