实体框架错误:字段"ForeignKey"是必需的EF

本文关键字:quot EF ForeignKey 字段 错误 实体 框架 | 更新日期: 2023-09-27 17:52:48

我有一个实体设置如下

public class ExampleObject
{
    [Key]
    public int ID { get; set; }
    public int RelationID { get; set; }
    public string SomeValue { get; set; }
    [ForeignKey("RelationID")]
    public virtual RelationObj RelationObj { get; set; }
}

在我的项目后面,我使用这个实体如下

foreach (var rec in allRecs)
{
    db.ExampleObject.Attach(rec);
    rec.SomeValue = "TEST"
}
db.SaveChanges();

这会抛出一个DbEntityValidationException "The field "RelationObj" is required"

我不想在加载这些记录时包含RelationObj,这会影响性能。EF为什么要费心去检查涉外对象?我该如何解决这个问题?

实体框架错误:字段"ForeignKey"是必需的EF

首先创建db,更改脚本顶部的数据库以匹配您的db

USE [Breaz]
GO
/****** Object:  Table [dbo].[Relation]    Script Date: 6/29/2016 4:35:32 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Relation](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [RelatedSomeValue] [varchar](10) NULL,
 CONSTRAINT [PK_Relation] 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

然后创建下一个表,再次修改db (brez):

USE [Breaz]
GO
/****** Object:  Table [dbo].[Example]    Script Date: 6/29/2016 4:34:15 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Example](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [RelationID] [int] NOT NULL,
    [SomeValue] [varchar](10) NULL,
 CONSTRAINT [PK_Example] 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].[Example]  WITH CHECK ADD  CONSTRAINT [FK_Example_Relation] FOREIGN KEY([RelationID])
REFERENCES [dbo].[Relation] ([ID])
GO
ALTER TABLE [dbo].[Example] CHECK CONSTRAINT [FK_Example_Relation]
GO

在每个表中添加几行数据

添加新项ADO。. NET实体数据模型,从您的数据库中添加这两个表。将edmx命名为ExampleModel。从Example.Context.cs文件中获取(BreazEntities7)上下文(你的上下文)。

 public class HomeController : Controller
{
    public ActionResult YIndex()
    {
        using (BreazEntities7 db = new BreazEntities7())
        {
            var allRecs = db.Examples;
            foreach (var rec in allRecs)
            {
                rec.SomeValue = "TEST";
            }
            db.SaveChanges();
        }
        return View();
}

下面是生成的示例:

namespace Testy2.Models
{
    using System;
    using System.Collections.Generic;
    public partial class Example
    {
        public int ID { get; set; }
        public int RelationID { get; set; }
        public string SomeValue { get; set; }
        public virtual Relation Relation { get; set; }
    }
}

下面是生成的关系:

namespace Testy2.Models
{
    using System;
    using System.Collections.Generic;
    public partial class Relation
    {
        public Relation()
        {
            this.Examples = new HashSet<Example>();
        }
        public int ID { get; set; }
        public string RelatedSomeValue { get; set; }
        public virtual ICollection<Example> Examples { get; set; }
    }
}