简体   繁体   中英

EF code first PK column - Cannot insert the value NULL into column / The INSERT statement conflicted with the FOREIGN KEY constraint

I am struggling with a relationship between my 2 entities. It should be a 1-to-1 relation where a File may or may not contain a OcrTask.

Here are the entities (I have removed most of the irrelevant properties)

 public class RepositoryFile 
    {
        public string FileName { get; set; }
        public int RepositoryFileId { get; set; }
        public int ProjectId { get; set; }
        public virtual Project Project { get; set; }
        public int? OcrTaskId { get; set; }
        public virtual OcrTask OcrTask { get; set; }
    }

This is the dependent entity:

public class OcrTask
{
    private OcrTask()
    {
    }
    public OcrTask(int repositoryFileId)
    {
        this.RepositoryFileId = repositoryFileId;
    }
    [JsonProperty]
    public int OcrTaskId { get; internal set; }
    public int RepositoryFileId { get; set; }
    public virtual RepositoryFile RepositoryFile { get; set; }
    public int ProjectId { get; set; }
    public virtual Project Project { get; set; }
}

And, for clarity, the project (which may contain zero or more files and therefore zero or more OcrTasks - but each task and file have to be associated with a project)

public class Project
    {
        internal Project()
        {
        }
        public Project(string projectName)
        {
            this.ProjectName = projectName;
        }
        public string ProjectName { get; set; }
        [JsonProperty]
        public int ProjectId { get; internal set; }
        public virtual ICollection<OcrTask> OcrTasks { get; set; } = new HashSet<OcrTask>();
        public virtual ICollection<RepositoryFile> RepositoryFiles { get; set; } = new HashSet<RepositoryFile>();
    }

Now, the problem I have is that when I am trying to insert an entry into the OcrTasks table using EntityFramework, I get an error saying The INSERT statement conflicted with the FOREIGN KEY constraint \\"FK_dbo.OcrTasks_dbo.Projects_ProjectId\\". The conflict occurred in database \\"TheDatabase\\", table \\"dbo.Projects\\" The INSERT statement conflicted with the FOREIGN KEY constraint \\"FK_dbo.OcrTasks_dbo.Projects_ProjectId\\". The conflict occurred in database \\"TheDatabase\\", table \\"dbo.Projects\\"

When I went to the database to try the insert from SQL studio, the error shows Cannot insert the value NULL into column 'OcrTaskId', table 'ConHubTestDb.dbo.OcrTasks'; column does not allow nulls. INSERT fails. Cannot insert the value NULL into column 'OcrTaskId', table 'ConHubTestDb.dbo.OcrTasks'; column does not allow nulls. INSERT fails.

The SQL I run is (file with id 4 and project id 5 already exist in DB)

insert into OcrTasks(CreatedDate, RepositoryFileId, ProjectId)
VALUES (GETUTCDATE(),4,5)

I don't understand why doesn't the identity column get assigned a value normally - all the other entities do?

Also, here's what columns have been generated by EF. When I check the properties of the OcrTaskId column, I see that Identity is set to False. Why?

在此处输入图片说明

Also, I have a fluent API piece. Without that, I get an error that cannot determine which is the dependent end of the relationshit between the File and Task entities.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RepositoryFile>().HasOptional(f => f.OcrTask).WithRequired(t => t.RepositoryFile);
            base.OnModelCreating(modelBuilder);
        }

Thanks in advance!

Part of your problem is OcrTaskId column have Indentity set false. You should set it true so you can have autoincrement. Then you insert ill work.

About the error of FOREIGN KEY, can you post the code where you are doing the entity insert?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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