【问题标题】:Table name created in EF, created into SQL with a different name [duplicate]在 EF 中创建的表名,以不同的名称创建到 SQL [重复]
【发布时间】:2019-10-03 16:05:34
【问题描述】:

我使用 EntityFramework 和 Code First 方法来创建我的项目所需的表。我的一张表在 c# 中被命名为“Forum”,但是当它在 MSSQL 中创建它时,将其命名为“Fora”...

我的所有表格都完全按照我在 c# 中命名的方式生成,除了上述情况,我检查了“论坛”不是保留关键字。而且我能够在名为“论坛”的新表中手动创建一个数据库和一个表。

我浏览了整个项目的代码,但找不到对“论坛”的引用。

现在从编码的角度来看,这不是一个真正的问题,我仍然在代码中以 dbContext.Forum.Get() 访问它,但是查看 ERD 会显示它为 Fora。

以前有没有其他人看到过这种情况?有什么具体的我做错了吗?

任何帮助将不胜感激:)

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityProject.Utilities;

namespace CommunityProject.Data.Models
{
    public enum CanCreateTopic
    {
        Admin,
        Moderator,
        All
    }

    public partial class Forum : Entity
    {
        public Forum()
        {
            CanCreateTopicId = CanCreateTopic.All;
            IsDeleted = false;
            CDate = DateTime.UtcNow;
            IsNewsForum = false;
        }

        [Index("IX_Forum_ID")]
        public int Id { get; set; }

        //Foreign Keys

        [Index("IX_Forum_CategoryID")]
        public int CategoryId { get; set; }

        public int CUserId { get; set; }

        public int? EUserId { get; set; }

        //Content

        /// <summary>
        /// The icon to display on the forum
        /// </summary>
        public string Icon { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public bool IsLocked { get; set; }

        public CanCreateTopic CanCreateTopicId { get; set; }

        public int SortOrder { get; set; }

        public bool IsDeleted { get; set; }

        /// <summary>
        /// If this is checked, the topics created here will be visible on the Home Page.
        /// </summary>
        public bool IsNewsForum { get; set; }

        public DateTime CDate { get; set; }

        public DateTime? EDate { get; set; }

        //Virtuals

        public virtual User CreatedUser { get; set; }

        public virtual User EditedUser { get; set; }

        public virtual Category Category { get; set; }

        public virtual IList<Topic> Topics { get; set; }

        public bool UserCanCreateTopics(string forumUserType)
        {
            switch (CanCreateTopicId)
            {
                case CanCreateTopic.All:
                    return true;
                case CanCreateTopic.Moderator:
                    switch (forumUserType)
                    {
                        case AppConstants.AdministratorTypeName:
                        case AppConstants.ModeratorTypeName:
                        case AppConstants.OwnerTypeName:
                        case AppConstants.SystemTypeName:
                            return true;
                        default:
                            return false;
                    }
                case CanCreateTopic.Admin:
                    switch (forumUserType)
                    {
                        case AppConstants.AdministratorTypeName:
                        case AppConstants.OwnerTypeName:
                        case AppConstants.SystemTypeName:
                            return true;
                        default:
                            return false;
                    }
                default:
                    return false;

            }
        }
    }
}

如果您需要更多信息,我会更新问题以添加它:3

【问题讨论】:

  • ForaForum 的复数形式,而不是 Forums。就像index 的复数形式是indices,而不是索引。 EF 的约定是将实体名称复数。看起来 EF 使用的复数包也可以正常工作......所以Forum 变成了Fora
  • 默认情况下,如果表名还不是复数,EF 会将表名复数。如果需要,可以覆盖它;全局在 OnModelCreating 中并单独使用 Table 属性。我更喜欢英语单词 Forum 的复数形式 Forums 并且不知道它的拉丁语起源,但你去吧。
  • 哦,哇,太糟糕了。英语不是我的第一语言,我以为会是论坛 :D 现在我觉得自己是一个不先检查的工具._.
  • 我刚刚检查了源代码。看看这里:github.com/mono/entityframework/blob/master/src/EntityFramework/…
  • forumsfora 都可以接受。 EF 显然更喜欢 fora,但对于现代用法,它是 in the minority

标签: c# sql entity-framework


【解决方案1】:

entity framework 默认将表名复数。 Fora 是 Forum 的正确复数形式。

您可以关闭复数形式 - 正确答案在以下问题中找到,也有讨论:

Entity Framework creates a plural table name, but the view expects a singular table name?

执行它的代码是sn-p

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

【讨论】:

  • 感谢您的贡献,但您不应重复或复制重复问题的答案。相反,应该像刚才所做的那样关闭问题。一旦你有足够的声望,你就可以自己参与这个过程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 2021-04-04
  • 2022-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多