MVC3 / EF - 更改模型...数据库架构不匹配

发布于 2024-10-22 03:43:00 字数 775 浏览 0 评论 0原文

我一直在关注 ASP.Net 电影数据库教程,一切都很顺利。

我刚刚更改了模型并添加了一个属性。请说我好奇,但我不想遵循仅删除数据库的指南 - 我想看看是否可以修改。

正确错误是由于不匹配而出现的 - 这是预期的。

我将评级列添加到数据库中,一切正常。

因为我想按照教程进行操作并了解 DropCreateDatabaseIfModelChanges - 但是,我只是收到错误无效的列名称“Ratings”。

接下来,我删除了评级列, 时间,ModelHash 条目没有改变,我不知道它如何知道现在和以前之间存在差异。

所以 - 1)我搞砸了什么吗?

2)我该如何解决?

3) 之前它怎么知道某些东西已经改变,但现在哈希值没有改变时却不知道?

4)您还有什么可以提供的建议吗?

I have been following the ASP.Net Movie Database Tutorial, and it was all going well.

I have just changed the model and added an attribute. Call me curious, but I didn't want to follow the guide of just drop the database - I wanted to see if I can modify.

The correct error came up about a mismatch - which was expected.

I added the ratings column to the database, and everything worked.

Next, I deleted the ratings column as I wanted to follow the tutorial and learn about DropCreateDatabaseIfModelChanges - however, I just get the error Invalid column name 'Ratings'.

In all this time, the ModelHash entry has not changed, and I have no idea how it know there is a difference between now or before.

So - 1) Have I screwed something up?

2) How can I fix?

3) How did it know before that something has changed, but not now when the hash hasn't changed?

4) Is there any additional advice you can give?

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

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

发布评论

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

评论(1

顾忌 2024-10-29 03:43:00

我认为您的描述一定不正确,因为尽管手动添加了 Rating 列,但正确的行为仍会引发异常。

此行为的工作原理如下:

  1. 创建数据库时,添加名为 EdmMetadata 的新表。
  2. EdmMetadata 包含两列 - IdModelHash
  3. 数据库创建的一部分是使用当前模型的哈希值存储单行。
  4. 如果数据库不是由当前上下文创建的,则上下文会在执行第一个操作之前执行查询以检索存储的模型哈希。检索到的 has 与上下文的当前哈希进行比较。
  5. 如果散列不相同并且数据库初始化程序不允许数据库重新创建,则会引发异常。
  6. 如果hash相同,则执行所需的数据库操作。

手动添加 Rating 列不会更改存储的哈希值,但模型的哈希值会有所不同。

通过删除 IncludeMetadataConvention 约定可以完全消除此行为:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove(modelBuilder.Conventions
            .OfType<IncludeMetadataConvention>().ToArray());
    }

I think you description must be incorrect because correct behavior will throw the exception despite of manually added Rating column.

This behavior works as follows:

  1. When database is created new table called EdmMetadata is added.
  2. EdmMetadata contains two columns - Id and ModelHash.
  3. Part of database creation is storing single row with hash of the current model.
  4. If database was not created by the current context the context executes a query to retrieve stored model hash before the first operation is executed. The retrieved has is compared with context's current hash.
  5. If hash is not the same and database recreation is not allowed by database initializer the exception is thrown.
  6. If hash is the same, required db operation is executed.

Adding manually Rating column will not change stored hash but hash of the model will be different.

This behavior can be completely removed by removing IncludeMetadataConvention convention:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove(modelBuilder.Conventions
            .OfType<IncludeMetadataConvention>().ToArray());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文