gpt4 book ai didi

sql-server - Entity Framework : Create a model from Dictionary 要映射到数据库表

转载 作者:行者123 更新时间:2023-12-03 03:21:19 26 4
gpt4 key购买 nike

之前我有一个名为 ApplicationConfiguration 的表,其中只有 [Key]、[Value] 列来存储一些配置数据。这是立即使用 SQL 查询进行查询的。

现在我打算使用 Entity Framework (EF)代码优先方法来查询这个表。该表的特殊之处在于该表在其生命周期内只有固定数量的行。只能更新“值”列。

因此,根据代码优先方法,我们必须首先编写 POCO 类及其属性,这些属性将映射到基础表中的列。但是,我希望有一个 Dictionary<> 结构来表示这些配置 KV 对。我担心的是,EF 是否能够针对特定对的值的任何更新触发更新查询。

此外,由于我使用的是“代码优先”方法,因此我希望在首次执行应用程序时动态创建表本身后添加一些种子数据(即固定行数及其初始内容)。

如果字典<>无法使用,请建议一些替代方案。提前致谢。

最佳答案

这样编码:

public class ApplicationConfiguration
{
public int Id { get; set; }
public string Key { get; set; }
public int Value { get; set; } // should be string, but I'm lazy
}

class Context : DbContext
{
internal class ContextInitializer : DropCreateDatabaseIfModelChanges<Context>
{
protected override void Seed(Context context)
{
var defaults = new List<ApplicationConfiguration>
{
new ApplicationConfiguration {Key = "Top", Value = 5},
new ApplicationConfiguration {Key = "Bottom", Value = 7},
new ApplicationConfiguration {Key = "Left", Value = 1},
new ApplicationConfiguration {Key = "Right", Value = 3}
};

// foreach (var c in defaults)
// context.ConfigurationMap.Add(c.Key, c); // by design, no IReadOnlyDictionary.Add

foreach (var c in defaults)
context.ApplicationConfigurations.Add(c);

base.Seed(context);
}
}

public Context()
{
Database.SetInitializer(new ContextInitializer());
}

private IDbSet<ApplicationConfiguration> ApplicationConfigurations
{
get { return Set<ApplicationConfiguration>(); }
}

public IReadOnlyDictionary<string, ApplicationConfiguration> ConfigurationMap
{
get { return ApplicationConfigurations.ToDictionary(kvp => kvp.Key, kvp => kvp); }
}
}

这样使用:

using (var context = new Context())
{
ReadConfigurationOnly(context.ConfigurationMap);
}

using (var context = new Context())
{
ModifyConfiguration(context.ConfigurationMap);
context.SaveChanges();
}

static void ReadConfigurationOnly(IReadOnlyDictionary<string, ApplicationConfiguration> configuration)
{
foreach (var k in configuration.Keys)
Console.WriteLine("{0} = {1}", k, configuration[k].Value);
}

static void ModifyConfiguration(IReadOnlyDictionary<string, ApplicationConfiguration> configuration)
{
foreach (var k in configuration.Keys)
configuration[k].Value++; // this is why I was lazy, using an int for a string
}

所以,我这样写——使用 int Value属性(property)而不是 string - 这样我就可以一遍又一遍地运行“以这种方式使用”代码,并每次都看到数据库更新,而不必想出其他方法来更改 Value以一种有趣的方式。

这里使用 IReadOnlyDictionary<string, ApplicatonConfiguration> 不太漂亮。而不是IReadOnlyDictionary<string, string> ,我们真正喜欢的方式,但这足以弥补我们可以轻松修改集合值而无需诉诸笨拙的 Set以字典作为输入的方法。当然,缺点是我们必须满足于 configuration[key].Value = "new value"而不是configuration[key] = "new value" ,但是 - 正如我所说 - 我认为这是值得的。

编辑

当当!我专门编写了这段代码来回答这个问题,但我想我非常喜欢它,我将把它添加到我的技巧包中......当我的公司从本地数据库转向 Azure 时,这将非常适合云中的实例,当前的app.config必须进入数据库。

现在我需要的是 ContextInitializer采取System.Configuration.ConfigurationManager作为 ctor 参数,以便从现有的 app.config 中播种新的数据库...

关于sql-server - Entity Framework : Create a model from Dictionary<TKey, TValue> 要映射到数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28949019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com