多个“1至0..1”关系模型 [英] multiple "1 to 0..1" relationship models

查看:141
本文介绍了多个“1至0..1”关系模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个教程从微软创建与EF4.1的一对一关系在教师和OfficeAssignment之间。但是现在我想为每位讲师添加一个主页(1到零或-1),如下所示:





我添加了与OfficeAssignment完全相同的Home模型(如上面的教程),但是当我尝试添加这些模型的控制器时,我得到错误已添加相同名称的项目。



所以我的模型设置不正确。
下面有什么问题?
如何在EF4.1中创建多个一对一关系?

  public class Instructor 
{
public Int32 InstructorID {get;组; }
public string LastName {get;组; }
public string FirstMidName {get;组; }
public virtual OfficeAssignment OfficeAssignment {get;组; }
public virtual Home首页{get;组;
}

public class OfficeAssignment
{
[Key]
public int InstructorID {get;组; }
public string Location {get;组; }
public virtual Instructor Instructor {get;组;
}

public class Home
{
[Key]
public int InstructorID {get;组; }
public string Location {get;组; }
public virtual Instructor Instructor {get;组; }
}

public class上下文:DbContext
{
public DbSet< OfficeAssignment> OfficeAssignments {get;组; }
public DbSet< Instructor>导师{get;组; }
public DbSet< Home>家园组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

modelBuilder.Conventions.Remove< PluralizingTableNameConvention>();
modelBuilder.Entity< Instructor>()
.HasOptional(p => p.OfficeAssignment)
.WithRequired(p => p.Instructor);
modelBuilder.Entity< Instructor>()
.HasOptional(p => p.Home).WithRequired(p => p.Instructor);
}


解决方案

看起来不像EF支持实际1到0..1的关系。你需要一个外键并将可选(int?)添加到主模型中。



所以我做了如下,它的作用就像一个魅力。

  public class Instructor 
{
public Int InstructorID {get;组; }
public string LastName {get;组; }
public string FirstMidName {get;组; }

public int? OfficeAssignmentID {get;组; }
public virtual OfficeAssignment OfficeAssignment {get;组; }

public int? HomeID {get;组; }
public virtual Home首页{get;组;

}

public class OfficeAssignment
{
public int OfficeAssignmentID {get;组; }
public string Location {get;组; }

}

public class Home
{
public int HomeID {get;组; }
public string Location {get;组; }
}


I am using this tutorial from microsoft to create a one-zero-to-one relationship with EF4.1 Between an Instructor and OfficeAssignment. This is working like a charm.

But now I want to add a Home for each Instructor (1 to zero-or-1) like in this:

I added the Home model exactly the same way as the OfficeAssignment (like in the tutorial above), but when I try to add controllers for these model, I get the error "An item with the same name has already been added".

So my model is set up incorrectly. What is wrong with the below? How do I create multiple one-to-zero-to-one relationships in EF4.1?

public class Instructor
{
    public Int32 InstructorID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public virtual OfficeAssignment OfficeAssignment { get; set; }
    public virtual Home Home { get; set; }
}

public class OfficeAssignment
{
    [Key]
    public int InstructorID { get; set; }
    public string Location { get; set; }
    public virtual Instructor Instructor { get; set; }
}

public class Home
{
    [Key]
    public int InstructorID { get; set; }
    public string Location { get; set; }
    public virtual Instructor Instructor { get; set; }
}

public class Context : DbContext
{
    public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
    public DbSet<Instructor> Instructors { get; set; }
    public DbSet<Home> Homes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

          modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
          modelBuilder.Entity<Instructor>()
            .HasOptional(p => p.OfficeAssignment)
            .WithRequired(p => p.Instructor);
          modelBuilder.Entity<Instructor>()
             .HasOptional(p => p.Home).WithRequired(p => p.Instructor);
}

解决方案

Doesn't look like EF supports real 1 to 0..1 relationship. You need a foreign key. And add the optional (int?) into the main model.

So I did this as follow, and it works like a charm.

public class Instructor
{
    public Int InstructorID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }

    public int? OfficeAssignmentID { get; set; }
    public virtual OfficeAssignment OfficeAssignment { get; set; }

    public int? HomeID { get; set; }
    public virtual Home Home { get; set; }

}

public class OfficeAssignment
{
    public int OfficeAssignmentID { get; set; }
    public string Location { get; set; }

}

public class Home
{
    public int HomeID { get; set; }
    public string Location { get; set; }
}

这篇关于多个“1至0..1”关系模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆