【发布时间】:2017-09-20 23:08:12
【问题描述】:
我有模特:
public class FlyerPage
{
public Guid Id { get; set; }
public string Picture { get; set; }
public int? Page { get; set; }
public string Text { get; set; }
public ICollection<string> Keywords { get; set; }
public Guid FlyerId { get; set; }
public virtual Flyer Flyer { get; set; }
}
我想将Keywords 映射到FlyerPage 表中的一列。
我认为将其拆分为用逗号分隔的值的最佳方法,例如
"one", "two", "three".
怎么做? 通过 FluentAPI。
编辑 - 到另一个表
The type 'ICollection<string>' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method
modelBuilder.Entity<FlyerPage>().Map(m =>
{
m.Property(p => p.Keywords);
m.ToTable("Keyword", "Flyers.Page");
});
编辑 2:好的,这项工作
modelBuilder.Entity<FlyerPage>().Map(m =>
{
m.Properties(p => p.Keywords);
m.ToTable("FlyerPageKeywords");
});
但是多对多如何连接呢?
【问题讨论】:
-
你为什么会选择这样做?
-
为什么?因为关键字在 Page 内,我不想创建另一个表来获取它。
-
你真的应该有一张不同的桌子。如果您懒得创建另一个表,那么编程可能不适合您!
标签: c# entity-framework