数据库和实体级别的身份不是 breeze 元数据中的身份

Identity in database and on entity level is not identity in breeze metadata

我有两个实体,BlogEntry 和 BlogBody,两者的设置方式相同,但根据 breeze 元数据,BlogEntry ID 字段不是身份。我不明白为什么。

到目前为止我做了什么:

我 运行 想不通了。我想请教一些帮助,该怎么做以及如何将 BlogEntry 实体的 ID 字段作为元数据中的标识!

Db tables:

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");

您的数据模型和映射不匹配。让我们看看映射:

modelBuilder.Entity<BlogEntry>()
            .HasRequired(t => t.BlogBody)
            .WithRequiredDependent(bb => bb.BlogEntry);

这是一对一的关联。 Entity Framework 期望这将由 BlogEntry table 实现,该 BlogEntry table 具有非身份主键,该主键也是 BlogBody 主键的外键(可能或可能不是身份,随便你)。所以 "screws up" 身份字段的元数据是 EF,它只是不假设它在那里并且它不做任何事情来确认(毕竟它是代码优先)。

但我认为这不是您想要的那种关联。如果您希望它完全是一个 1:1 关联,则 BlogEntry 更可能是主要实体(可以独立存在的实体)。这也是数据模型所表达的。

但是 BlogBody 应该具有非身份 PK/FK,您应该相应地修改数据模型。

另一方面,如果数据模型是 leading/immutable,您唯一能做的就是将其映射为常规的一对多关联...

modelBuilder.Entity<BlogEntry>()
            .HasMany(be => be.BlogBodies)
            .WithRequired(bb => bb.BlogEntry)
            .HasForeignKey(bb => bb.BlogEntryId);

...并根据业务规则强制执行 BlogBodies 的数量不能 > 1.