이 제한을 극복하기 위해 "StringEntity"클래스를 작성하여 실험하기로 결정했으며 암시 적 연산자를 사용하여 문자열을 쉽게 변환 할 수있었습니다. 이 (무거운이기는하지만) 몇 가지 테스트를 완벽하게 작동 것처럼
public class MyClass
{
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public Guid Id { get; set; }
public List<StringEntity> Animals { get; set; }
public MyClass()
{
List<StringEntity> Animals = List<StringEntity>();
}
}
public class StringEntity
{
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public Guid Id { get; set; }
public string Value { get; set; }
public StringEntity(string value) { Value = value; }
public static implicit operator string(StringEntity se) { return se.Value; }
public static implicit operator StringEntity(string value) { return new StringEntity(value); }
}
public class MyDbContext : DbContext
{
public DbSet<MyClass> MyClasses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyClass>()
.HasMany(x => x.Animals)
.WithMany()
.Map(x =>
{
x.MapLeftKey(l => l.Id, "MyClassId");
x.MapRightKey(r => r.Id, "StringEntityId");
});
}
}
이 ... 모든 모습, 그리고 나는 MVC3보기에, 원래의 목적을 위해 다중 선택 목록 상자를 구현 : 솔루션은 아래를 참조하십시오. ListBox에 동일한 NAME이 Entity Collection Property로 할당 된 경우 알 수없는 이유 때문에 선택한 항목이로드되지 않습니다.
작동하지 않았다 다음을 설명하기 위해 :
//#1 Unpluralize the ListBox name so that is doesn't match the name Model.Animals
var animalOptions = new string[] {"Dog", "Cat", "Goat"};
@Html.ListBox("Animal", new MultiSelectList(animalOptions, Model.Animals.Select(x => x.Value)))
//#2 Use JQuery to replace the id and name attribute, so that binding can occur on the form post
<script type="text/javascript">
jQuery(function ($) {
$("select#Animal").attr("name", "Animals").attr("id", "Animals");
});
</script>
//#3 Create a model binder class to handle List<StringEntity> objects
public class StringEntityListBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stringArray = controllerContext.HttpContext.Request.Params.GetValues(bindingContext.ModelName);
return stringArray.Select(x => new StringEntity(x)).ToList();
}
}
//#4 Initialize the binder in your Global.asax setup.
ModelBinders.Binders.Add(typeof(List<StringEntity>), new StringEntityListBinder());
참고 목록 상자가 : // 면도기 코드보기
string[] animalOptions = new string[] {"Dog", "Cat", "Goat"};
string[] animalSelections = new string[] {"Dog", "Cat"};
Html.ListBox("Animals", Multiselect(animalOptions, animalSelections));
를이 제한을 해결하기 위해, 나는 네 가지를 할 필요가 속성이 문자열 목록 일 때 버그가 발생하지 않았으며 항목 목록 일 때 버그가 발생했습니다.
예제가 작동하지 않습니다. LOCAL 스칼라 변수의 집합을 참조 할 수 있지만 특정 엔터티의 경우 비 엔터티의 컬렉션 속성을 참조 할 수 없습니다. 예를 들어, p.FavorieColor (string)가있는 경우 "filterBy.Contains (p.FavoriteColor)"를 사용할 수 있지만 collection 속성을 통해 반복 할 수는 없습니다. –