1

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

4
  • Are you having problems between taskassignment and task or between the former and employee? Commented Feb 21, 2016 at 14:50
  • task_assignment and employee Commented Feb 21, 2016 at 14:52
  • But the exception you posted is about task-taskassignment not about employee, which actually it's 1:N between employee and assignment Commented Feb 21, 2016 at 14:57
  • its gone after I used this code modelBuilder.Entity<Task>().HasOptional(t => t.TaskAssignment).WithRequired(); Commented Feb 21, 2016 at 15:11

1 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

6 Comments

after removing tas_ass_id I end up with this \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'TaskAssignment' has no key defined. Define the key for this EntityType. \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'TaskAssignments' is based on type 'TaskAssignment' that has no keys defined.
My suggested code maps tas_id as key as you can see. Maybe you have a mistake there. If you search any on one to one configuration you will see plenty of examples
i ve tried your code. it give error on MapKey. so I replaced it with HasKey but same error
Yea sorry typo, corrected as HasKey. But if it doesn't work you ace another problem, maybe you could update your code. Could you show how are calling modelBuilder? Are you sure this code is running?
i am calling this in TaskAssignmentDBContext. and yes you can see modelBuilder in code now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.