简体   繁体   English

如何在Entity Framework + SQL Server中实施一对一关系

[英]How to enforce a one to one relationship in Entity Framework + SQL Server

Building my own mini CMS back end in EF + SQL Server (Database First). 在EF + SQL Server(数据库优先)中构建自己的迷你CMS后端。 I have a "Contents" table which all entities link too using a Foreign Key. 我有一个“目录”表,所有实体也都使用外键链接。

CREATE TABLE [dbo].[Contents](
[ContentID] [int] IDENTITY(1,1) NOT NULL,
[ContentType] [varchar](100) NOT NULL,
[DateAdded] [datetime] NOT NULL,
[DateUpdated] [datetime] NULL,
[isDeleted] [bit] NOT NULL,
[UserId] [int] NOT NULL

CREATE TABLE [dbo].[Articles](
[ArticleID] [int] IDENTITY(1,1) NOT NULL,
[ContentID] [int] NOT NULL,
[ImageID] [int] NOT NULL,
[ArticleHeadline] [varchar](250) NOT NULL,
[ArticleSummary] [nvarchar](200) NOT NULL,
[ArticleBody] [nvarchar](500) NOT NULL

ALTER TABLE [dbo].[Articles]  WITH CHECK ADD  CONSTRAINT [FK_Articles_Contents] FOREIGN KEY([ContentID])
REFERENCES [dbo].[Contents] ([ContentID])

In Entity Framework the Entity Content quite rightly thinks that this is a one to many relationship. 在实体框架中,实体内容非常正确地认为这是一对多的关系。 How do I go about making this one to one? 我该如何一对一制作? Content can only belong to one other Entity but I keep having to use First() in my code. 内容只能属于另一个实体,但是我必须在代码中继续使用First()。

<h1>@Model.Content.Articles.First().ArticleHeadline</h1>
@Html.Raw(Model.Content.Articles.First().ArticleBody)

As for database first approach, to have 1-1 association in EF, the dependent ([dbo].[Articles]) must have a primary key that also is the foreign key to the principal([dbo].[Contents]). 对于数据库优先方法,要在EF中具有1-1关联,从属([dbo]。[Articles])必须具有主键,该主键也是主体的外键([dbo]。[Contents])。 It will require some DB design change like this: 它将需要一些数据库设计更改,如下所示:

CREATE TABLE [dbo].[Contents](
[ContentID] [int] NOT NULL PRIMARY KEY,
[ContentType] [VARCHAR](100) NOT NULL
)

CREATE TABLE [dbo].[Articles](
[ContentID] [int] NOT NULL PRIMARY KEY, 
[ImageID] [int] NOT NULL,
CONSTRAINT [FK_Contents] FOREIGN KEY ([ContentID]) REFERENCES [dbo].[Contents] ([ContentID]) ON DELETE CASCADE
)

This should answer your question http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx 这应该可以回答您的问题http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

Look at the fluent configuration 看流利的配置

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
     // Configure StudentId as PK for StudentAddress 

modelBuilder.Entity<StudentAddress>() .HasKey(e => e.StudentId); 
    // Configure StudentId as FK for StudentAddress 

modelBuilder.Entity<StudentAddress>() .HasRequired(ad => ad.Student) .WithOptional(s => s.StudentAddress); }

Replace student address and student with article content and article 用文章内容和文章替换学生地址和学生

For db first mappings you will need to modify your edmx and regenerate your models. 对于数据库优先映射,您将需要修改edmx并重新生成模型。

This may help: http://www.exceptionnotfound.net/entity-framework-for-beginners-creating-a-database-first-model/ 这可能会有所帮助: http : //www.exceptionnotfound.net/entity-framework-for-beginners-creating-a-database-first-model/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM