2013-06-19 7 views
3

엔티티 프레임 워크 5를 사용하는 모델 내에 간단한 배열이 있습니다. 배열은 테이블 내에 저장되지 않습니다.Entity Framework 5는 배열 문자열을 저장하지 않습니다.

모델

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

구성

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

이니셜

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

이 '하지 않습니다 왜 모든 힌트 일. MyArray not 이벤트가 열로 나타납니다.

+4

당신은 당신이 배열 인 열 유형이 데이터베이스를 알 수 있습니까? 그렇지 않습니다. Entity Framework는 아마도 그렇게하지 않으므로 열로 생성되지 않습니다. 정규화 된 모델에서는 "가치"를 저장하는 다른 엔티티/테이블과 MyModel과의 일대 다 관계가 있어야합니다. –

+1

@ RaphaëlAlthaus 네, 그 가능한, NHibernate. 당신은 또한 JSON을 blobing하는 것과 같은 멋진 일을 할 수있다. http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/ – Phill

+0

@ Raphaël Althaus는 직렬화되었거나 특별한 것은 없다고 예상 했었습니다. 다른 O/R 매퍼를 사용합니다. –

답변

1

대신 문자열 배열을 사용하는 대신 ';'과 같은 구분 기호를 사용하여 간단한 문자열 열을 사용할 수 있습니다. 또는 '|', 아무거나는 할 것이다. 데이터베이스에서 데이터를 검색 할 때 C# String.Split 메서드를 호출하고 매개 변수와 동일한 구분 기호를 전달해야합니다. 예를 들어

:

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

string[] stringsRetrieved = columnValue.Split('|'); 
관련 문제