简体   繁体   English

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

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

I have an entity setup as follows 我有一个实体设置如下

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

Later in my project I use this entity as follows 稍后在我的项目中,我使用此实体如下

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

db.SaveChanges();

This throws a DbEntityValidationException "The field "RelationObj" is required" 抛出DbEntityValidationException“字段”RelationObj“是必需的”

I don't want to include the RelationObj when loading these records, that affects performance. 我不想在加载这些记录时包含RelationObj,这会影响性能。 Why is EF bothering to check the foreign relation object? 为什么EF打扰检查外来关系对象? How do I go about fixing this? 我该如何解决这个问题?

First create the db, change the database on top of the script to match your 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

Then create the next table, again change the db (Breaz): 然后创建下一个表,再次更改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

Add a couple of rows of data into each table. 在每个表中添加几行数据。

Add new item ADO.NET Entity Data Model, from your db and add both tables. 从您的数据库添加新项ADO.NET实体数据模型并添加两个表。 Name the edmx ExampleModel. 将edmx命名为ExampleModel。 Get the (BreazEntities7) context (your context) from the Example.Context.cs file. 从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();
}

Here is the generated Example: 这是生成的示例:

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

Here id the generated Relation: 在这里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