【发布时间】: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
【问题讨论】:
-
Fora是Forum的复数形式,而不是Forums。就像index的复数形式是indices,而不是索引。 EF 的约定是将实体名称复数。看起来 EF 使用的复数包也可以正常工作......所以Forum变成了Fora -
默认情况下,如果表名还不是复数,EF 会将表名复数。如果需要,可以覆盖它;全局在
OnModelCreating中并单独使用Table属性。我更喜欢英语单词Forum的复数形式Forums并且不知道它的拉丁语起源,但你去吧。 -
哦,哇,太糟糕了。英语不是我的第一语言,我以为会是论坛 :D 现在我觉得自己是一个不先检查的工具._.
-
forums 和 fora 都可以接受。 EF 显然更喜欢 fora,但对于现代用法,它是 in the minority。
标签: c# sql entity-framework