简体   繁体   中英

Entity framework code first one-to-one relation between complex entities

Guys I am new to Entity Framework, I have no Idea how to make relations using model builder. I have tried to create relation between task and task_assignment by using this following code block.

modelBuilder.Entity<Task>().HasOptional(t => t.TaskAssignment).WithRequired();

I want to create one-to-one relation between Task_Assignment and Employee. I have tried to make relation but every approach leads me to another error. The most frequest ones are here.

A dependent property in a ReferentialConstraint is mapped to a store generated column. Column: 'tas_ass_id'.

I have following database schema.

task table

CREATE TABLE [dbo].[Task] (
    [tas_id]              INT           IDENTITY (1, 1) NOT NULL,
    [tas_name]            VARCHAR (50)  NOT NULL,
    PRIMARY KEY CLUSTERED ([tas_id] ASC),
);

Task Assignment table

CREATE TABLE [dbo].[Task_Assignment] (
    [tas_ass_id]            INT IDENTITY (1, 1) NOT NULL,
    [tas_id]                INT NOT NULL,
    [person_responsible_id] INT NOT NULL,
    [person_notified_id]    INT NULL,
    [person_approval_id]    INT NULL,
    PRIMARY KEY CLUSTERED ([tas_ass_id] ASC),
    CONSTRAINT [FK_Task_Assignment_Task] FOREIGN KEY ([tas_id]) REFERENCES [dbo].[Task] ([tas_id]),
    CONSTRAINT [FK_Task_Assignment_EmployeeR] FOREIGN KEY ([person_responsible_id]) REFERENCES [dbo].[Employee] ([emp_id]),
    CONSTRAINT [FK_Task_Assignment_EmployeeN] FOREIGN KEY ([person_notified_id]) REFERENCES [dbo].[Employee] ([emp_id]),
    CONSTRAINT [FK_Task_Assignment_EmployeeA] FOREIGN KEY ([person_approval_id]) REFERENCES [dbo].[Employee] ([emp_id])
);

Employee table

CREATE TABLE [dbo].[Employee] (
    [emp_id]                        INT           IDENTITY (1, 1) NOT NULL,
    [emp_name]                      VARCHAR (60)  NOT NULL,NULL,
    PRIMARY KEY CLUSTERED ([emp_id] ASC),

);

Model for task

class Task{
     public int tas_id {get; set;}
     public virtual TaskAssignment TaskAssignment {get; set;}
}

Model for Task Assignment

class TaskAssignment{
     [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int tas_ass_id { get; set; }
    [Required]
    [Display(Name = "Task")]
    public int tas_id { get; set; }
    [ForeignKey("tas_id")]
    public Task Task { get; set; }

    [Required]
    [Display(Name = "Person Responsible")]
    public int person_responsible_id { get; set; }
    [ForeignKey("person_responsible_id")]
    public Employee Responsible { get; set; }

    [Display(Name = "Person Notified")]
    public int person_notified_id { get; set; }
    [ForeignKey("person_notified_id")]
    public Employee Notified { get; set; }

    [Display(Name = "Person Approval")]
    public int person_approval_id { get; set; }
    [ForeignKey("person_approval_id")]
    public Employee Approval { get; set; }
}

Model for Employee

class Employee{
     public int emp_id {get; set;}
}

Model builder

protected override void OnModelCreating(DbModelBuilder modelBuilder)
         {
             //configure model with fluent API 
             modelBuilder.Entity<Task>().HasOptional(t => t.TaskAssignment).WithRequired();
             modelBuilder.Entity<TaskAssignment>().HasKey(x=>x.tas_id);

             modelBuilder.Entity<Task>().HasOptional(x=>x.TaskAssignment).WithRequired(x=>x.Task);
         }

I am really exhausted now and I have no idea what to do? please guide me. Thanks in advance

The relation between TaskAssignment and Employee, according to your db scheme, is one to many (which sounds logical as one employee could be assigned to many tasks usually). Your problem doesn't seems to be here but between Task and TaskAssignment.

About standard entity framework one to zero-one relationships, dependent entity TaskAssignment) uses the same entity id as fk to the primary entity (Task) so your tas_id column should be PK of TaskAssignment and fk to Task, and tas_ass_id should be removed from table and class. Code for one to zero-one configuration:

builder.Entity<TaskAssignment>()
    HasKey(x=>x.tas_id);

builder.Entity<Task>()
    .HasOptional(x=>x.TaskAssignment)
    .WithRequired(x=>x.Task);

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