繁体   English   中英

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

[英]Entity Framework Error: The field “ForeignKey” is required EF

我有一个实体设置如下

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“字段”RelationObj“是必需的”

我不想在加载这些记录时包含RelationObj,这会影响性能。 为什么EF打扰检查外来关系对象? 我该如何解决这个问题?

首先创建数据库,在脚本之上更改数据库以匹配您的数据库

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(Breaz):

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; }
    }
}

在这里id生成的Relation:

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; }
    }
}

暂无
暂无

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

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