下面是DDL:
USE [Breaz]
GO
/****** Object: Table [dbo].[diplome] Script Date: 10/6/2016 1:33:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[diplome](
[Id] [int] IDENTITY(1,1) NOT NULL,
[titre] [varchar](10) NULL,
[description] [varchar](10) NULL,
[personneId] [int] NULL,
CONSTRAINT [PK_diplome] 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 OFF
GO
/****** Object: Table [dbo].[personne] Script Date: 10/6/2016 1:33:31 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[personne](
[Id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](10) NULL,
CONSTRAINT [PK_personne] 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 OFF
GO
ALTER TABLE [dbo].[diplome] WITH CHECK ADD CONSTRAINT [FK_diplome_personne] FOREIGN KEY([personneId])
REFERENCES [dbo].[personne] ([Id])
GO
ALTER TABLE [dbo].[diplome] CHECK CONSTRAINT [FK_diplome_personne]
GO
然後可以使用實體框架(EDMX)ADO.NET實體數據模型。這將生成的表,因爲你指定:
public partial class personne
{
public personne()
{
this.diplomes = new HashSet<diplome>();
}
public int Id { get; set; }
public string name { get; set; }
public virtual ICollection<diplome> diplomes { get; set; }
}
public partial class diplome
{
public int Id { get; set; }
public string titre { get; set; }
public string description { get; set; }
public Nullable<int> personneId { get; set; }
public virtual personne personne { get; set; }
}
這裏是控制器方法:
public ActionResult Index2()
{
BreazEntities entity = new BreazEntities();
var perso = entity.personnes.ToList();
return View(perso);
}
這裏是視圖:
@{
Layout = null;
}
<!DOCTYPE html>
@model IEnumerable<Testy20161006.personne>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index2</title>
</head>
<body>
<div>
@foreach (var pers in Model)
{
<section>
<header>Person is @pers.name</header>
@foreach (var item in pers.diplomes)
{
<p>Dipl: @item.titre</p>
}
</section>
}
</div>
</body>
</html>