我有一个使用MVC5与身份和实体框架的项目。 我首先使用数据库,并使用ASPNetUser.Id创建了许多表作为FK。 所以,例如我有这张表:
CREATE TABLE [dbo].[Logs](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[LogTypeId] [smallint] NOT NULL,
[Description] [varchar](5000) NOT NULL,
[DateTime] [datetime] NOT NULL,
[UserId] [nvarchar](128) NULL,
CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
ALTER TABLE [dbo].[Logs] ADD CONSTRAINT [DF_Log_DateTime] DEFAULT (getdate()) FOR [DateTime]
GO
ALTER TABLE [dbo].[Logs] WITH CHECK ADD CONSTRAINT [FK_Log_AspNetUsers] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
GO
ALTER TABLE [dbo].[Logs] CHECK CONSTRAINT [FK_Log_AspNetUsers]
GO
ALTER TABLE [dbo].[Logs] WITH CHECK ADD CONSTRAINT [FK_Log_LogType] FOREIGN KEY([LogTypeId])
REFERENCES [dbo].[LogTypes] ([Id])
GO
ALTER TABLE [dbo].[Logs] CHECK CONSTRAINT [FK_Log_LogType]
GO
所以,我创建了我的EntityFramework 6 edmx,我添加了所有表格,包括这一个。
之后,我使用Scaffolding作为CRUD,它很好地创建了Controller和Views。
但是...当我播放所有项目时,在索引视图中它会在行上抛出错误:
public ActionResult Index()
{
var logs = db.Logs.Include(l => l.LogType);
return View(logs.ToList()); //----->>> HERE THROWS THE ERROR
}
错误:
无效的列名称'AspNetUser_Id'。
查询尝试运行:
SELECT
1 AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[LogTypeId] AS [LogTypeId],
[Extent1].[Description] AS [Description],
[Extent1].[DateTime] AS [DateTime],
[Extent1].[UserId] AS [UserId],
[Extent2].[Id] AS [Id1],
[Extent2].[Description] AS [Description1],
[Extent1].[AspNetUser_Id] AS [AspNetUser_Id] //---> THIS FIELD DOES NOT EXISTS !!!
FROM [dbo].[Logs] AS [Extent1]
INNER JOIN [dbo].[LogTypes] AS [Extent2] ON [Extent1].[LogTypeId] = [Extent2].[Id]
那么,你知道为什么要寻找ASPNetUser_Id吗?该字段不存在。 而且,为什么其他FK工作正常?
我试过没有成功:
任何想法???
感谢所有人!