3

i have a simple array inside a model using the entity framework 5. The array will not saved inside the table.

Model

public class MyModel: IEntity
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public IList<string> MyArray { get; set; }
}

Config

    DbContext.Configuration.ProxyCreationEnabled = true;
    DbContext.Configuration.LazyLoadingEnabled = true;
    DbContext.Configuration.ValidateOnSaveEnabled = false;

Initializer

internal class DatabaseInitializer : DropCreateDatabaseIfModelChanges<DatabaseContext>
{
    protected override void Seed(DatabaseContext context)
    {
        context.MyModels.Add(new MyModel {
           MyArray = new [] { "Value1", "Value2" }
        });
   }
}

Any hint why this does' not work. MyArray not event appear as a column.

9
  • 4
    do you know a database where you have a column type which is an array ? I don't, Entity Framework probably doesn't either, so it's not created as a column. In a normalized model, you should have another entity / table to save your "Values" and a one to many relationship with MyModel. Commented Jun 19, 2013 at 11:32
  • 1
    @RaphaëlAlthaus Yes, its possible, NHibernate. You can also do fancy stuff with it like blobbing JSON. philliphaydon.com/2012/03/… Commented Jun 19, 2013 at 11:35
  • @Raphaël Althaus had expected that it got serialized, nothing special... and it's to late to use a different o/r mapper. Commented Jun 19, 2013 at 11:39
  • @Phill column type in db is not an array in your example, and we're talking about entity framework. So ? By the way, even if possible, it's a very bad way (normalization, even when using an ORM, is a rather good thing - I think). Commented Jun 19, 2013 at 11:40
  • @Abc then don't declare it as an array in your model, just as a string, and make serialization yourself ? Commented Jun 19, 2013 at 11:41

1 Answer 1

1

Instead of using an array of strings, you can alternatively use a simple string column with some kind of separator like ';' or '|', anything will do. Just make sure that when you retrieve your data from the database you call the C# String.Split method, passing to it the same separator as parameter.

For example:

string stringToBePersisted = "First Value|Second Value|Third Value";

string[] stringsRetrieved = columnValue.Split('|');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.