问题描述
我的任务很简单,我需要创建一个库mvc网络应用程序,允许粗暴操作,唯一的约束是1本书可以有多个作者,也可以有多个发布商。
my task is very simple, i need to create a library mvc web app that allows crud operations, the only constraints are that 1 book can have multiple authors and can also have multiple publishers.
来完成这个,这是我的数据库模型:
to accomplish this, this is my database model:
和书籍 - > books_authors->作者表关系的相应sql代码,发布商关系以相同的方式定义
and the respective sql code for the books->books_authors->authors table relationships, the publishers relationship is defined in the same way
CREATE TABLE [dbo].[BOOKS](
[ID] [int] NOT NULL,
[TITLE] [nvarchar](max) NOT NULL,
[PAGES] [int] NOT NULL,
[SYNOPSIS] [nvarchar](max) NOT NULL,
[PUBLISHDATE] [date] NOT NULL,
[ISBN] [nvarchar](max) NOT NULL,
[SUBJECT] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_BOOKS] 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] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[BOOKS_AUTHORS](
[BOOKID] [int] NOT NULL,
[AUTHORID] [int] NOT NULL,
CONSTRAINT [PK_BOOKS_AUTHORS] PRIMARY KEY CLUSTERED
(
[BOOKID] ASC,
[AUTHORID] 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
ALTER TABLE [dbo].[BOOKS_AUTHORS] WITH CHECK ADD CONSTRAINT [FK_BOOKS_AUTHORS_AUTHORS] FOREIGN KEY([AUTHORID])
REFERENCES [dbo].[AUTHORS] ([ID])
GO
ALTER TABLE [dbo].[BOOKS_AUTHORS] CHECK CONSTRAINT [FK_BOOKS_AUTHORS_AUTHORS]
GO
ALTER TABLE [dbo].[BOOKS_AUTHORS] WITH CHECK ADD CONSTRAINT [FK_BOOKS_AUTHORS_BOOKS1] FOREIGN KEY([BOOKID])
REFERENCES [dbo].[BOOKS] ([ID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[BOOKS_AUTHORS] CHECK CONSTRAINT [FK_BOOKS_AUTHORS_BOOKS1]
GO
CREATE TABLE [dbo].[AUTHORS](
[ID] [int] NOT NULL,
[FIRSTNAME] [nvarchar](max) NOT NULL,
[LASTNAME] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_AUTHORS] 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] TEXTIMAGE_ON [PRIMARY]
GO
现在我的问题,当我添加一个ado.net模型到mvc网络应用程序,edmx看起来像这样:
我以为我定义了一对多的关系BOOKS和BOOKS_之间作者/ BOOKS_PUBLISHERS,为什么现在显示在edmx?
我的另一个问题是,鉴于上述edmx模型,当我创建具有视图的脚手架控制器,创建操作将BOOKS中的ID字段作为输入列出,而不是在索引操作上隐藏相同的ID字段,如下所示:
推荐答案
实体框架edmx,不显示参考关系表。
这就是为什么它显示书籍和作者之间的许多关系
Entity Framework edmx, do not show the Reference Relationship Table.that is why Its showing Many to Many relationship between book and authors
根据是纯连接表
第二期是因为您尚未定义主键作为标识(自动生成)
2nd Issue is because, you have not defined your primary key as Identity (Auto Generated)
这篇关于mvc数据库首先发布与edmx / relationships和查看显示id字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!