在EF中创建的表名,在SQL中创建的表名不同

时间:2019-05-16 11:27:36

标签: c# sql entity-framework

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

除上述情况外,我所有表的生成方式均与我在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;

            }
        }
    }
}

Screenshot of Tables

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

1 个答案:

答案 0 :(得分:0)

实体框架默认情况下将表名复数。 论坛是论坛的正确复数。

您可以关闭复数-在以下问题中也可以通过讨论找到正确的答案:

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

做到这一点的代码段

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