实体框架 - 代码优先 - 无法存储 List<String> [英] Entity Framework - Code First - Can&#39;t Store List&lt;String&gt;

查看:26
本文介绍了实体框架 - 代码优先 - 无法存储 List<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.我没有任何适用于 Strings 列表的表格或关系.

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

我做错了什么?我也尝试制作 Strings virtual 但它没有改变任何东西.

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

感谢您的帮助.

推荐答案

Entity Framework 不支持原始类型的集合.您可以创建一个实体(将保存到不同的表中)或进行一些字符串处理以将您的列表保存为字符串并在实体具体化后填充列表.

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.

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

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