c# - How to enforce a one to one relationship in Entity Framework + SQL Server -


building own mini cms end in ef + sql server (database first). have "contents" table entities link using 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]  check add  constraint [fk_articles_contents] foreign key([contentid]) references [dbo].[contents] ([contentid]) 

in entity framework entity content quite rightly thinks 1 many relationship. how go making 1 one? content can belong 1 other entity keep having use first() in code.

<h1>@model.content.articles.first().articleheadline</h1> @html.raw(model.content.articles.first().articlebody) 

as database first approach, have 1-1 association in ef, dependent ([dbo].[articles]) must have primary key foreign key principal([dbo].[contents]). require db design change 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 ) 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -