为什么这两个模型之间存在零或一对多的关系?

时间:2017-03-24 18:07:12

标签: asp.net-mvc entity-framework relationship

我正在构建ASP.NET MVC6的模型,
我正在进行的设计很简单:一门课程可能有一位或多位教师。
这是我的模特:

public class Instructor
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }
    public string Bio { get; set; }
}
public class Course
   {
       [Required]
       public int Id { get; set; }
       [Required]
       public string Title { get; set; }
       [Required]
       public string Description { get; set; }
   }

这是定义课程与教师之间关系的课程:

 public class Course_Instructor_Relationship
    {
        [Required]
        public int Id { get; set; }
        [Required]
        public int CourseId { get; set; }
        [Required]
        public int InstructorId{ get; set; }
        [Required]
        public virtual ICollection<Instructor> Instructors { get; set; }
        public virtual Course Course { get; set; }
    }

正如我在模型图中看到的那样,zero or one to manyCourse_Instructor_Relationship之间存在Instructor关系,而我期待one to many关系。
我无法弄清楚为什么。

1 个答案:

答案 0 :(得分:1)

零或一对多关系,因为您没有使课程实体成为必需,因此它可以为null,这就是为什么EF生成关系为零或一对多的原因。

我认为,从您的实体名称,一个课程可以有一个或多个教师,一个教师可以在零到多个课程。如果这是正确的,您可以按照以下步骤操作:

function hcol = CPU_monitor
h = create_gui;
end


function mon = createMonitor
MatlabProcess = System.Diagnostics.Process.GetCurrentProcess(); %// "Matlab" process
cpuIdleProcess = 'Idle';
mon.NumOfCPU = double(System.Environment.ProcessorCount);
mon.ProcPerfCounter.Matlab  = System.Diagnostics.PerformanceCounter('Process', '% Processor Time', MatlabProcess.ProcessName);
mon.ProcPerfCounter.cpuIdle = System.Diagnostics.PerformanceCounter('Process', '% Processor Time', cpuIdleProcess);
end

function updateMeasure(obj,evt,hfig)

h = guidata(hfig);

%// Calculate the cpu usage
cpu.total = 100 - h.mon.ProcPerfCounter.cpuIdle.NextValue / h.mon.NumOfCPU;
cpu.matlab = h.mon.ProcPerfCounter.Matlab.NextValue / h.mon.NumOfCPU;
dlmwrite('cpulog.txt', [toc, cpu.total , cpu.matlab], '-append');

   %// update the display
set(h.txtTotalCPU,'String',num2str(cpu.total,'%5.2f %%'))
set(h.txtMatlabCPU,'String',num2str(cpu.matlab,'%5.2f %%'))
end

function StartMonitor(obj,evt)
h = guidata(obj);
start(h.t)
tic
end

function StopMonitor(obj,evt)
h = guidata(obj);
stop(h.t)
data=dlmread('cpulog.txt');
data(1,:)=[]; 
figure;plot(data(:,1),data(:,2),data(:,1) ,data(:,3));
legend('total cpu','matlab cpu'); xlabel('sec'); ylabel('%');
end

function h = create_gui %// The boring part
h.fig = figure('Unit','Pixels','Position',[200 800 240 120],'MenuBar','none','Name','CPU usage %','NumberTitle','off');
h.btnStart = uicontrol('Callback',@StartMonitor,'Position',[10 80 100 30],'String', 'START');
h.btnStart = uicontrol('Callback',@StopMonitor,'Position',[130 80 100 30 ],'String', 'STOP');
h.lbl1 = uicontrol('HorizontalAlignment','right','Position',[10 50 100 20],'String','TOTAL :','Style','text');
h.txtTotalCPU = uicontrol('Position',[130 50 100 20],'String','0','Style','text');
h.lbl2 = uicontrol('HorizontalAlignment','right','Position',[10 10 100 20],'String','Matlab :','Style','text');
h.txtMatlabCPU = uicontrol('Position',[130 10 100 20],'String','0','Style','text');
movegui(h.fig,'center')

%// create the monitor
h.mon = createMonitor;

%// Create the timer
h.t = timer;
h.t.Period = 0.25;
h.t.ExecutionMode = 'fixedRate';
h.t.TimerFcn = {@updateMeasure,h.fig};
h.t.TasksToExecute = Inf;

%// store the handle collection
guidata(h.fig,h)
end

如果没有:你可以这样做:

public class Instructor
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }
    public string Bio { get; set; }

    public List<Course> Courses{get;set;}
}

public class Course
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }
    [Required]
    public List<Instructor> Instructors{get;set;}
}

您还可以指定映射以准确指定所需的关系:

public class Instructor
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }
    public string Bio { get; set; }

    public Course Course{get;set;}
}

public class Course
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }

    public virtual IEnumerable<Instructor> Instructors{get;set;}
}

或者你想要的:

public class InstructorMap : EntityTypeConfiguration<Instructor>
{
    public override void Map(EntityTypeBuilder<Instructor> builder)
    {
        builder.HasKey(a => a.Id);

        builder.HasMany(i => i.Courses);

        builder.ToTable("Instructors");
    }
}


public class CourseMap : EntityTypeConfiguration<Course>
{
    public override void Map(EntityTypeBuilder<Course> builder)
    {
        builder.HasKey(a => a.Id);

        builder.HasMany(i => i.Instructors);

        builder.ToTable("Courses");
    }
}