【问题标题】:Storing IEnumerable<string> in Entity Framework在实体框架中存储 IEnumerable<string>
【发布时间】:2014-11-09 08:07:27
【问题描述】:

是否可以在实体框架中存储IEnumerable&lt;string&gt;

我在 ASP.NET MVC5 中使用代码优先,我有一个看起来有点像这样的模型,但 ImageUris 在我的数据库中没有作为列出现(所有其他属性都出现)。

public class Product
{
    [Key]
    public int Id { get; set; }

    public string Title { get; set; }
    public string Description { get; set; }
    public string Condition { get; set; }
    public decimal Price { get; set; }

    public IEnumerable<string> ImageUris { get; set; }
}

PS:如果您对我为什么要存储 Uris 而不是图像本身感兴趣,它们是 Azure 存储 Blob 的 uri。

【问题讨论】:

标签: entity-framework asp.net-mvc-5


【解决方案1】:

您不能在关系数据库的单个列中保存多条记录。没有这样的数据类型支持这一点。

您可以为 Image Uris 创建一个单独的表,然后将您的图像 Uris 存储在那里。

您的实体代码如下所示:

public class Product
{
    [Key]
    public int Id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public string Condition { get; set; }

    public decimal Price { get; set; }

    public virtual ICollection<ImageUri> ImageUris { get; set; }
}

public class ImageUri
{
    [Key]
    public int Id { get; set; }

    public string Uri { get; set; }
}

【讨论】:

  • 您确定 EF 不会自动创建表来存储字符串吗?
  • 不,这永远不会发生,实体框架会按照您的代码执行的操作。
猜你喜欢
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多