我有两个实体,BlogEntry和BlogBody,两者都以相同的方式设置,但BlogEntry ID字段根据breeze元数据不是标识。我不明白为什么。
到目前为止我做了什么:
我已经没想完了。我想问一些帮助做什么,以及如何将BlogEntry实体的ID字段作为元数据中的标识!
Db表:
CREATE TABLE [dbo].[BLOG_BODY](
[ID] [int] IDENTITY(1,1) NOT NULL,
[BODY_WITH_MARKUP] [text] NOT NULL,
[BODY_WITHOUT_MARKUP] [text] NULL,
[BLOG_ENTRY_ID] [int] NOT NULL,
[CREATE_DATE] [datetimeoffset](7) NOT NULL DEFAULT (getdate()),
[CREATED_BY] [int] NOT NULL DEFAULT ((1)),
[LAST_CHANGED_DATE] [datetimeoffset](7) NOT NULL DEFAULT (getdate()),
[LAST_CHANGED_BY] [int] NOT NULL DEFAULT ((1)),
CONSTRAINT [PK_dbo.BLOG_BODY] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 75) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[BLOG_BODY] WITH CHECK ADD CONSTRAINT [FK_dbo.Blog_Body__Blog_Entry_Id] FOREIGN KEY([BLOG_ENTRY_ID])
REFERENCES [dbo].[BLOG_ENTRY] ([ID])
GO
ALTER TABLE [dbo].[BLOG_BODY] CHECK CONSTRAINT [FK_dbo.Blog_Body__Blog_Entry_Id]
GO
CREATE TABLE [dbo].[BLOG_ENTRY](
[ID] [int] IDENTITY(1,1) NOT NULL,
[TITLE] [nvarchar](255) NOT NULL,
[IS_PUBLISHED] [int] NOT NULL DEFAULT ((0)),
[AUTHOR_ID] [int] NOT NULL DEFAULT ((1)),
[CREATE_DATE] [datetimeoffset](7) NOT NULL DEFAULT (getdate()),
[CREATED_BY] [int] NOT NULL DEFAULT ((1)),
[LAST_CHANGED_DATE] [datetimeoffset](7) NOT NULL DEFAULT (getdate()),
[LAST_CHANGED_BY] [int] NOT NULL DEFAULT ((1)),
CONSTRAINT [PK_dbo.BLOG_ENTRY] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 75) ON [PRIMARY]
) ON [PRIMARY]
实体:
public class BlogBody
{
public int Id { get; set; }
public string BodyWithMarkup { get; set; }
public string BodyWithoutMarkup { get; set; }
public int BlogEntryId { get; set; }
public virtual BlogEntry BlogEntry { get; set; }
}
public class BlogEntry
{
public int Id { get; set; }
[Required]
[StringLength(255, MinimumLength = 3)]
public string Title { get; set; }
[Required]
public int IsPublished { get; set; }
public virtual BlogBody BlogBody { get; set; }
}
映射:
#region BLOG_ENTRY
modelBuilder.Entity<BlogEntry>().ToTable("BLOG_ENTRY");
modelBuilder.Entity<BlogEntry>().HasKey(k => k.Id);
modelBuilder.Entity<BlogEntry>().Property(t => t.Id).HasColumnName("ID");
modelBuilder.Entity<BlogEntry>().Property(t => t.Title).HasColumnName("TITLE");
modelBuilder.Entity<BlogEntry>().Property(t => t.IsPublished).HasColumnName("IS_PUBLISHED");
modelBuilder.Entity<BlogEntry>().HasRequired(t => t.BlogBody).WithRequiredDependent(bb => bb.BlogEntry);
//AUTHOR_ID is not mapped yet
#endregion
#region BLOG_BODY
modelBuilder.Entity<BlogBody>().ToTable("BLOG_BODY");
modelBuilder.Entity<BlogBody>().HasKey(k => k.Id);
modelBuilder.Entity<BlogBody>().Property(t => t.Id).HasColumnName("ID");
modelBuilder.Entity<BlogBody>().Property(t => t.BodyWithMarkup).HasColumnName("BODY_WITH_MARKUP");
modelBuilder.Entity<BlogBody>().Property(t => t.BodyWithoutMarkup).HasColumnName("BODY_WITHOUT_MARKUP");
modelBuilder.Entity<BlogBody>().Property(t => t.BlogEntryId).HasColumnName("BLOG_ENTRY_ID");
答案 0 :(得分:1)
您的数据模型和映射不匹配。让我们看一下映射:
modelBuilder.Entity<BlogEntry>()
.HasRequired(t => t.BlogBody)
.WithRequiredDependent(bb => bb.BlogEntry);
这是一对一的关联。实体框架期望这由BlogEntry
表实现,该表具有非标识主键,该主键也是BlogBody
主键的外键(可能是也可能不是标识, 随你喜欢)。因此,EF&#34;搞砸&#34;身份字段的元数据,它只是没有假设它在那里,它没有做任何事情来确认(它的代码首先,毕竟)。
但我不认为这是你想要的那种联想。如果您希望这是1:1关联,则BlogEntry
更可能是主要实体(可以单独存在的实体)。这也是数据模型表达的内容。
但是BlogBody
应该有非身份PK / FK,你应该相应地修改数据模型。
另一方面,如果数据模型是领先/不可变的,那么您唯一能做的就是将其映射为常规的一对多关联......
modelBuilder.Entity<BlogEntry>()
.HasMany(be => be.BlogBodies)
.WithRequired(bb => bb.BlogEntry)
.HasForeignKey(bb => bb.BlogEntryId);
...并按业务规则执行,BlogBodies
的数量不能为&gt; 1。