Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当需要[一对二]关系时,如何映射实体?_C#_Entity Framework - Fatal编程技术网

C# 当需要[一对二]关系时,如何映射实体?

C# 当需要[一对二]关系时,如何映射实体?,c#,entity-framework,C#,Entity Framework,一个账户有两个地址:永久账户和流动账户。我不能将Address类用作复杂类型,因为我需要一个数据库表来处理它,并实现更简单的设计。我还有其他实体需要地址实体(一对一) 处理这种情况的标准方法是什么?如何映射这些实体 如果解决方案使用实体框架代码优先约定来实现这种关系,那就太好了。如果您想获得一对多关系,您需要定义一个类,该类将包含另一个类的对象列表。 例如,我有两个实体学生和标准。我希望这个标准应该包含很多学生。在本例中,我使用下一个代码: class Address { // Prope

一个账户有两个地址:永久账户和流动账户。我不能将Address类用作复杂类型,因为我需要一个数据库表来处理它,并实现更简单的设计。我还有其他实体需要地址实体(一对一)

处理这种情况的标准方法是什么?如何映射这些实体


如果解决方案使用实体框架代码优先约定来实现这种关系,那就太好了。

如果您想获得一对多关系,您需要定义一个类,该类将包含另一个类的对象列表。 例如,我有两个实体学生和标准。我希望这个标准应该包含很多学生。在本例中,我使用下一个代码:

class Address { 
  // Properties 
}
class Account
{
  //Other Properties
  Public Address Permanent { get; set; }
  Public Address Current { get; set; }
}
class Other
{
  //Other Properties
  Public Address OtherAddress { get; set; }
}
公共班级学生
{
公立学生(){}
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟标准{get;set;}
}
公共类标准
{
公共标准()
{
StudentsList=新列表();
}
公共int标准ID{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection学生{get;set;}
}

如果要获得一对多关系,需要定义一个类,该类将包含另一个类的对象列表。 例如,我有两个实体学生和标准。我希望这个标准应该包含很多学生。在本例中,我使用下一个代码:

class Address { 
  // Properties 
}
class Account
{
  //Other Properties
  Public Address Permanent { get; set; }
  Public Address Current { get; set; }
}
class Other
{
  //Other Properties
  Public Address OtherAddress { get; set; }
}
公共班级学生
{
公立学生(){}
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟标准{get;set;}
}
公共类标准
{
公共标准()
{
StudentsList=新列表();
}
公共int标准ID{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection学生{get;set;}
}

如果要获得一对多关系,需要定义一个类,该类将包含另一个类的对象列表。 例如,我有两个实体学生和标准。我希望这个标准应该包含很多学生。在本例中,我使用下一个代码:

class Address { 
  // Properties 
}
class Account
{
  //Other Properties
  Public Address Permanent { get; set; }
  Public Address Current { get; set; }
}
class Other
{
  //Other Properties
  Public Address OtherAddress { get; set; }
}
公共班级学生
{
公立学生(){}
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟标准{get;set;}
}
公共类标准
{
公共标准()
{
StudentsList=新列表();
}
公共int标准ID{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection学生{get;set;}
}

如果要获得一对多关系,需要定义一个类,该类将包含另一个类的对象列表。 例如,我有两个实体学生和标准。我希望这个标准应该包含很多学生。在本例中,我使用下一个代码:

class Address { 
  // Properties 
}
class Account
{
  //Other Properties
  Public Address Permanent { get; set; }
  Public Address Current { get; set; }
}
class Other
{
  //Other Properties
  Public Address OtherAddress { get; set; }
}
公共班级学生
{
公立学生(){}
公共int StudentId{get;set;}
公共字符串StudentName{get;set;}
公共虚拟标准{get;set;}
}
公共类标准
{
公共标准()
{
StudentsList=新列表();
}
公共int标准ID{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection学生{get;set;}
}

我不是EF方面的专家,但我认为你可以这样做

public class Student
    {
        public Student() { }

        public int StudentId { get; set; }
        public string StudentName { get; set; }

        public virtual Standard Standard { get; set; }
    }

    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }

我不是EF方面的专家,但我认为你可以这样做

public class Student
    {
        public Student() { }

        public int StudentId { get; set; }
        public string StudentName { get; set; }

        public virtual Standard Standard { get; set; }
    }

    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }

我不是EF方面的专家,但我认为你可以这样做

public class Student
    {
        public Student() { }

        public int StudentId { get; set; }
        public string StudentName { get; set; }

        public virtual Standard Standard { get; set; }
    }

    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }

我不是EF方面的专家,但我认为你可以这样做

public class Student
    {
        public Student() { }

        public int StudentId { get; set; }
        public string StudentName { get; set; }

        public virtual Standard Standard { get; set; }
    }

    public class Standard
    {
        public Standard()
        {
            StudentsList = new List<Student>();
        }
        public int StandardId { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }

实际上最简单的方法是

 public class Account
 {
    [Key]
    public int Id {get;set;}

    ...

    public virtual PermanetAddress PermenentAddress {get;set;}
    public virtual CurrentAddress CurrentAddress {get;set;}
  }

  class Address
  {
     //properties
  }

  class PermanetAddress : Address
  {
     [ForeignKey("Account")]
     public int Id {get;set;}
     //properties
     public virtual Account account {get;set;}
  }
  class CurrentAddress : Address
  {
     [ForeignKey("Account")]
     public int Id {get;set;}
     //properties
     public virtual Account account {get;set;}
  }
类地址{
//性质
}
类别帐户
{
//其他属性
公共int永久地址{get;set;}
Public int CurrentAddressId{get;set;}
公共广播永久{get;set;}
当前公共广播{get;set;}
}
其他类
{
//其他属性
Public int OtherAddressId{get;set;}
公共地址其他地址{get;set;}
}
类AccountMapping:EntityTypeConfiguration
{
公共帐户映射()
{
总额(“账户”);
//其他人喜欢哈斯基
HasRequired(a=>a.ParmanentAddress).WithMany().HasForeignKey(p=>p.PermanentAddressId);
HasRequired(a=>a.CurrentAddress).WithMany().HasForeignKey(p=>p.CurrentAddressId);
}
}
类OtherMapping:EntityTypeConfiguration
{
公共映射()
{
HasRequired(a=>a.OtherAddress).WithMany().HasForeignKey(p=>p.OtherAddressId);
}
}

实际上,最简单的方法是

 public class Account
 {
    [Key]
    public int Id {get;set;}

    ...

    public virtual PermanetAddress PermenentAddress {get;set;}
    public virtual CurrentAddress CurrentAddress {get;set;}
  }

  class Address
  {
     //properties
  }

  class PermanetAddress : Address
  {
     [ForeignKey("Account")]
     public int Id {get;set;}
     //properties
     public virtual Account account {get;set;}
  }
  class CurrentAddress : Address
  {
     [ForeignKey("Account")]
     public int Id {get;set;}
     //properties
     public virtual Account account {get;set;}
  }
类地址{
//性质
}
类别帐户
{
//其他属性
公共int永久地址{get;set;}
Public int CurrentAddressId{get;set;}
公共广播永久{get;set;}
当前公共广播{get;set;}
}
其他类
{
//其他属性
Public int OtherAddressId{get;set;}
公共地址其他地址{get;set;}
}
类AccountMapping:EntityTypeConfiguration
{
公共帐户映射()
{
总额(“账户”);
//其他人喜欢哈斯基
HasRequired(a=>a.ParmanentAddress).WithMany().HasForeignKey(p=>p.PermanentAddressId);
HasRequired(a=>a.CurrentAddress).WithMany().HasForeignKey(p=>p.CurrentAddressId);
}
}
类OtherMapping:EntityTypeConfiguration
{
公共映射()
{
HasRequired(a=>a.OtherAddress).WithMany().HasForeignKey(p=>p.OtherAddressId);
}
}

实际上,最简单的方法是

 public class Account
 {
    [Key]
    public int Id {get;set;}

    ...

    public virtual PermanetAddress PermenentAddress {get;set;}
    public virtual CurrentAddress CurrentAddress {get;set;}
  }

  class Address
  {
     //properties
  }

  class PermanetAddress : Address
  {
     [ForeignKey("Account")]
     public int Id {get;set;}
     //properties
     public virtual Account account {get;set;}
  }
  class CurrentAddress : Address
  {
     [ForeignKey("Account")]
     public int Id {get;set;}
     //properties
     public virtual Account account {get;set;}
  }
类地址{
//性质
}
类别帐户
{
//其他属性
公共int永久地址{get;set;}
Public int CurrentAddressId{get;set;}
公共广播永久{get;set;}
当前公共广播{get;set;}
}
其他类
{
//其他属性
P