0

I am attempting to persist an object with a delegate property

public delegate void Callback(ScheduledTask ScheduledEvent);
public class ScheduledTask
    {
        [Key]
        public int Id { get; set; }

        /// <summary>
        /// Time the event will be triggered
        /// </summary>
        public DateTime ScheduledTime { get; set;} 


        /// <summary>
        /// Delegate to call back
        /// </summary>
        public Callback callback { get; set; }

        /// <summary>
        /// Any event metadata
        /// </summary>
        public Dictionary<String, object> Metadata { get; set; }
    }

However i receive this error:

(12,10) : error 3004: Problem in mapping fragments starting at lines 6, 12, 21: No mapping specified for properties ScheduledTask.callback in Set ScheduledTasks. An Entity with Key (PK) will not round-trip when: Entity is type [Frontline.Core.Scheduling.ScheduledTask]

when loading the context.

How do i persist an object with a delegate using entity framework?

MrJD
  • 1,879
  • 1
  • 23
  • 40

2 Answers2

0

I can't see how what you are asking for could work. A delegate is runtime information linking 2 objects. The only way to get close to this would be to completely serialize the object graph and store that somewhere.

See here for a bit more info; Could we save delegates in a file (C#)

I think you'd be better rethinking your design - you could store an ID to some other object which is the subscriber to Callback, and re-connect the objects after loading them back from the database. However, if the subscriber could effectively be anything in your system, that will complicate the database design.

Community
  • 1
  • 1
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • Agree - the only *possible* way I could see this working is if you stored "unbound delegates" (i.e., explicitly passing all values used in as arguments), and even then, you'd have so many issues crop up if any of the Types used changed in the slightest... – JerKimball Jan 31 '13 at 02:35
0

I ended up serialising it to a binary stream and saving that. Kind of gross, but its working.

    public byte[] callbackData { get; set; }

    /// <summary>
    /// Delegate to call back
    /// </summary>
    [NotMapped]
    public Callback callback
    {
        get
        {
            if (_callback == null && callbackData != null && callbackData.Count() > 0)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (var stream = new MemoryStream(callbackData))
                {
                    _callback = formatter.Deserialize(stream) as Callback;
                }
            }
            return _callback;
        }
        set
        {
            _callback = value;
            if (value == null)
            {
                callbackData = null;
            }
            else
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (var stream = new MemoryStream())
                {
                    formatter.Serialize(stream, value);
                    callbackData = stream.ToArray();
                }
            }
        }
    }
MrJD
  • 1,879
  • 1
  • 23
  • 40