【问题标题】:Seed Entity Framework Model with List Member具有列表成员的种子实体框架模型
【发布时间】:2016-06-25 21:38:08
【问题描述】:

我有一个带有列表成员的实体框架模型:

public class MyModel
{
        public int Id { get; set; }
        public string Name { get; set; }
        public int Points { get; set; }
        public List<Feature> Features { get; set; }
}

Feature 是一个枚举。

我的配置中有一个带有对象文字的Seed() 方法:

new MyModel
{
    Id = 1,
    Name = "Test Name",
    Points = 2,
    Features = ???
}

如何将功能初始化为文字列表?

【问题讨论】:

  • 看到答案(到目前为止)似乎没有人注意到您不能在数据库中存储List&lt;Feature&gt;。想想看,目标数据库字段应该有哪种数据类型? Feature 必须是一个类,映射到自己的表。
  • 感谢 @GertArnold - 为我完成了业余时间,忘记了 enums 与实体框架。我在下面更新了我的答案,现在应该可以解决 OP 的问题 :)

标签: c# entity-framework


【解决方案1】:

如何将功能初始化为文字列表?

更新
正如@Gert 正确指出的那样,您不能直接在 EF 数据库中使用枚举(哦,drats)

但是,enumint 表示,因此该值仍然可以通过使用类存储在数据库中。

您需要创建一个类以便将值存储在数据库中,然后您可以从 MyModel 类中引用它。

像这样:

public enum TypesOfFeature // The enum of features
{
    One = 1,
    Two = 2,
    Three = 3
    ... // etc.
}

public class Feature // The class to map to a table in your DB
{
    public int Id { get; set; }
    public TypesOfFeature Type { get; set; }
}

DbSet&lt;Feature&gt; 添加到您的代码优先模型中,以便您可以从数据库中访问它。像public DbSet&lt;Feature&gt; Features { get; set; } 这样的东西就可以了。

手动将值添加到数据库中可能是个好主意,这样您就知道它们的 ID 将匹配枚举的 int 值。 *您可以Seed() 方法中使用AddOrUpdate 执行此操作 - 然后当您需要添加更多内容时,可以在下方添加它们*

您需要从数据库返回您想要的Features,然后将它们分配给您的MyModel.Features 属性。

像这样:

Feature featureOne = YourDbContext.Features.Single(x => x.Type == TypesOfFeature.One);
Feature featureTwo = YourDbContext.Features.Single(x => x.Type == TypesOfFeature.Two);
Feature featureThree = YourDbContext.Features.Single(x => x.Type == TypesOfFeature.Three);

然后,在代码中,您正在初始化 MyModel 对象,然后您可以初始化 List&lt;Feature&gt;,传入您在上面提取的所需 Feature 对象:

var model = new MyModel
{
    Id = 1,
    Name = "Test Name",
    Points = 2,
    Features = new List<Feature>
               {
                   featureOne,
                   featureTwo,
                   featureThree
                }
}

显然,我不知道你的Feature enum 中有什么值,但上面只是一个例子。

希望这会有所帮助! :)

【讨论】:

  • 如何在MyModel 种子中使用Feature?我使用context.Features.AddOrUpdate 在种子中添加Feature 对象。然后我做context.SaveChanges(),然后是context.MyModel.AddOrUpdate,我通过new MyModel { ..., Features = new List&lt;Feature&gt; { context.Features.Single(x =&gt; x.Type == TypesOfFeature.One) } }。问题是当我从context 抓取MyModel 对象时,它们有一个空的Features 列表。
  • 您究竟在哪里尝试使用MyModel.Features 并遇到此问题?您正在编写什么类型的程序(App、MVC 等)? Features 是空的还是 null
  • 我有一个控制台应用程序,我添加了实体框架。从我的主要方法中,我创建了一个上下文并通过执行var firstMyModel = context.MyModel.First() 获取一个对象,问题是firstMyModel.Featuresnull
  • 您是通过调试器还是通过控制台窗口的输出找到的?突然想到(我在移动设备上,所以空间有限):您可能希望将您的 Features 声明为导航属性。一个快速的谷歌会告诉你这个。我可能会建议研究 EF 中的延迟加载/急切加载以及如何使用它。您还可以使用显式加载。例如:在查询中选择MyModel 对象,使用context.MyModels.Include(x =&gt; x.Features) 从数据库中获取它们。您需要在代码顶部包含 using System.Data.Entity;
【解决方案2】:

将其初始化为枚举列表

 var model = new MyModel
        {
            Id = 1,
            Name = "Test Name",
            Points = 2,
            Features = new List<Feature>
            {
                Feature.Feature1,
                Feature.Feature2
            }
        };

【讨论】:

  • 错了。 List&lt;Feature&gt; 无法存入数据库。
【解决方案3】:
static void Main(string[] args)
    {
        MyModel mm = new MyModel();

        mm.Id = 1;
        mm.Name = "user3728304";
        mm.Points = 1;
        mm.Features = new List<Feature>{Feature.one,
                         Feature.two,
                         Feature.three};

        Console.Read();
    }

【讨论】:

  • 错了。 List&lt;Feature&gt; 无法存入数据库。
  • @GertArnold 您可以将其存储在不同的表中,例如 Model_Feature bu 使用模型 id 和特征,一对多关系 b\w 模型和特征。
  • 是的,但你的答案没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多