0

我创建了一个名为Tag.

public class Tag
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TagId { get; set; }

    public string Name { get; set; }

    public virtual ICollection<string> Synonyms { get; set; }
}

我如何将Synonyms属性放入数据库?

我必须为它创建一个模型吗?

4

2 回答 2

1

是的,您必须创建一个Model,也许这样做:

public class Tag
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TagId { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Synonym> Synonyms { get; set; }
}

然后你的同义词模型:

public class Synonym
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [HiddenInput(DisplayValue = false)]
    public int SynonymId { get; set; }

    [ForeignKey("Tag"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int TagId { get; set; }

    public string Name { get; set; }

    public virtual Tag Tag { get; set; }
}
于 2013-03-17T18:38:24.027 回答
1

您必须将其包装在实体类中

例如

public class Synonym
{
  int SynonymId {get; set;}
  string Synonym {get; set;}
}

或将您的收藏展平为单独的字符串字段

例如:

public class Tag
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int TagId { get; set; }

    public string Name { get; set; }
    public string SynStore {get; set;}
    public IEnumerable<string> Synonyms 
{
    set { SynStore = String.Join(',', value);}
    get{ return SynStore.split(','); }
}
}
于 2013-03-17T18:39:36.067 回答