1

I want to use EF code first to create a column into a table Task, which is an array. How?

public class Task
{
     // my presumption code
     public string[] Attempts { get; set; }

The Attempts has

   AttemptsMetadata---maybe string
   Time            ---DataTime
   Answered        ---bool

1 Answer 1

1

Create a property to be used in the code (and mark as ignore) and other property to be used in code.

EDITED

public class Task
{
    [Ignore]
    public string[] Attempts { get; set; }

    public string AttemptsMetadata
    {
        get
        {
            return Attempts != null && Attempts.Any()
                ? Attempts.Aggregate((ac, i) => ";" + ac + i).Substring(1)
                : null;
        }
        set { Attempts = value.Split(';'); }
    }
}

PS:
This strategy has a one flaw. When you use repository expressions you cannot use the ignore property. But I never find another way to do so.

Sign up to request clarification or add additional context in comments.

2 Comments

How about others such as Time? It is not a string. I mean should I define Attempt as an object rather than a string array?
If you have a array of primitive types, you can use this approach, but if you using a non primitive type I will advise you to create a table to store this info and you use foreign key to create a relationship. In LINQ this relationship will be represented like a ICollection<> property (it's not a array but is very closer to).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.