实体框架-代码优先-无法存储列表< String> [英] Entity Framework - Code First - Can't Store List<String>

查看:61
本文介绍了实体框架-代码优先-无法存储列表< String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这样的课程:

class Test
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public List<String> Strings { get; set; }

    public Test()
    {
        Strings = new List<string>
        {
            "test",
            "test2",
            "test3",
            "test4"
        };
    }
}

internal class DataContext : DbContext
{
    public DbSet<Test> Tests { get; set; }
}

运行代码后:

var db = new DataContext();
db.Tests.Add(new Test());
db.SaveChanges();

我的数据已保存,但仅 Id 。我没有适用于字符串列表的任何表或关系。

my data is getting saved but just the Id. I don't have any tables nor relationships applying to Strings list.

我在做什么错?我也尝试过制作 Strings ,但是它并没有改变任何东西。

What am I doing wrong? I tried also to make Strings virtual but it didn't change anything.

谢谢您的帮助。

推荐答案

实体框架不支持原始类型的集合。您可以创建一个实体(将其保存到另一个表中),也可以进行一些字符串处理以将列表另存为字符串,并在实体实现后填充列表。

Entity Framework does not support collections of primitive types. You can either create an entity (which will be saved to a different table) or do some string processing to save your list as a string and populate the list after the entity is materialized.

这篇关于实体框架-代码优先-无法存储列表&lt; String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆