如何在模型优先上调用Database.SetInitializer?

发布于 2024-12-17 04:26:51 字数 333 浏览 6 评论 0原文

我使用“DbContext Entity Generator”作为生成项在 edm 设计器 (VS10) 中创建了一个模型。

在生成的 DbContext 子类中,它覆盖了构造函数,因此我无法在其他部分类中使用它:

public EntitiesContainer()
    : base("name=EntitiesContainer")
{
    this.Configuration.LazyLoadingEnabled = false;
}

What's theproperwayofinitialingadatabasewithmodel-first?

I created a model in the edm designer (VS10) using "DbContext Entity Generator" as the generation item.

In the generated DbContext subclass, it overrode the constructor so I can't use it in the other partial class:

public EntitiesContainer()
    : base("name=EntitiesContainer")
{
    this.Configuration.LazyLoadingEnabled = false;
}

What's the proper way of initialize a database with model-first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-12-24 04:26:51

您可以更改用于生成 DbContxt 类的 T4 模板。然后,您可以添加要用于初始化上下文的部分修饰符或方法。

You could alter the T4 template that's being used for generating the DbContxt class. Then you could add the partial modifiers or methods that you want to use to initialize your context.

贪了杯 2024-12-24 04:26:51

首次使用模型时不会自动初始化数据库。您必须从模型生成 SQL 脚本(使用 EDMX 设计器中的上下文菜单并选择从模型生成数据库)并在现有数据库上自行执行。

SetInitializer 仅适用于代码优先。

There is no automatic database initialization when using model first. You must generate SQL script from the model (use context menu in EDMX designer and choose Generate database from model) and execute it yourselves on existing database.

SetInitializer is only for code first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文