实体框架代码首先在复杂实体

时间:2016-02-21 13:58:18

标签: c# entity-framework asp.net-mvc-4

伙计我是Entity Framework的新手,我不知道如何使用模型构建器建立关系。我试图通过使用以下代码块来创建task和task_assignment之间的关系。

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

我想在Task_Assignment和Employee之间创建一对一的关系。我试图建立关系,但每种方法都会导致我另一个错误。 最常见的是在这里。

  

ReferentialConstraint中的依赖属性映射到商店   生成列。专栏:&#39; tas_ass_id&#39;。

我有以下数据库架构。

任务表

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

任务分配表

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])
);

员工表

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),

);

任务模型

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

任务分配模型

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

员工模型

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

模型构建器

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

我现在真的很累,我不知道该怎么办?请指导我。提前致谢

0 个答案:

没有答案