上下文不会将实体映射到父级

时间:2019-08-21 09:44:09

标签: c# .net-core entity-framework-core

我正在尝试使用EF-Core从数据库中显示一些数据。提供的数据已经使用EF-core插入到数据库中,并且可以正常工作。 我想使用telephones and emails输出关联了EF-core的Person的JSON,但只获取不包含telephones and emails的JSON。

这是我的实体:

    public class Person
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public enum Geschlecht
        {
            U = 0,
            W = 1,
            M = 2
        }
        [MaxLength(255)]
        public string TitelVor { get; set; }
        [Required]
        [MaxLength(255)]
        public string Name { get; set; }
        [Required]
        [MaxLength(255)]
        public string Vorname { get; set; }
        [MaxLength(255)]
        public string TitelNach { get; set; }
        [MaxLength(255)]
        public string Zusatz { get; set; }
        public DateTime Geburtsdatum { get; set; }
        public string Notiz { get; set; }
        public List<EMail> EMails { get; set; } = new List<EMail>();
        public List<Telefon> Telefone { get; set; } = new List<Telefon>();
    }
    public class Telefon
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required]
        [ForeignKey("AdresstypId")]
        public Adresstyp Adresstyp { get; set; }
        public int AdresstypId { get; set; }
        [ForeignKey("TelefontypId")]
        public Telefontyp Telefontyp { get; set; }
        public int TelefontypId { get; set; }
        [Required]
        [MaxLength(10)]
        public string Vorwahl { get; set; }
        [Required]
        [MaxLength(20)]
        public string Nummer { get; set; }
        [Required]
        [MaxLength(20)]
        public string Durchwahl { get; set; }
        [Required]
        public bool Primär { get; set; }
        [Required]
        [ForeignKey("PersonId")]
        public Person Person { get; set; }
        public int PersonId { get; set; }
    }
    public class EMail
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required]
        [ForeignKey("AdresstypId")]
        public Adresstyp Adresstyp { get; set; }
        public int AdresstypId { get; set; }
        [Required]
        [MaxLength(255)]
        public string Mail { get; set; }
        [Required]
        public bool Primär { get; set; }
        [Required]
        [ForeignKey("PersonId")]
        public Person Person { get; set; }
        public int PersonId { get; set; }
    }

这是我的上下文:

    public class AdressverwaltungContext : DbContext
    {
        public DbSet<Person> Personen { get; set; }
        public DbSet<Adresse> Adressen { get; set; }
        public DbSet<EMail> EMails { get; set; }
        public DbSet<Adresstyp> Adresstypen { get; set; }
        public DbSet<Telefon> Telefon { get; set; }
        public DbSet<Telefontyp> Telefontyp { get; set; }

        public AdressverwaltungContext(DbContextOptions<AdressverwaltungContext> options) : base(options)
        {
            Database.Migrate();
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Person>()
                        .HasMany(x => x.Telefone)
                        .WithOne(y => y.Person);
            modelBuilder.Entity<Person>()
                        .HasMany(x => x.EMails)
                        .WithOne(y => y.Person);

        }
    }

这是我的控制器:

public class DummyController : Controller
{
    private AdressverwaltungContext adressverwaltungContext;

    public DummyController(AdressverwaltungContext ctx)
    {
        adressverwaltungContext = ctx;
    }

    [HttpPost]
    [Route("api/GetAllPersons")]
    public JsonResult GetAllPersons()
    {
        return Json(adressverwaltungContext.Personen);
    }
}

我正在使用Postman显示我的API的结果,并在调用方法“ GetAllPersons”时获得以下结果:

[
    {
        "id": 3,
        "titelVor": "test",
        "name": "name",
        "vorname": "vorname",
        "titelNach": "titelnach",
        "zusatz": "zusatz",
        "geburtsdatum": "1990-01-01T00:00:00",
        "notiz": "test",
        "eMails": [],
        "telefone": []
    }
]

您可以看到telefone and emails为空。 我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您可以在查询中使用Include(),以使EF知道您有兴趣将其带入相关实体。像这样:

adressverwaltungContext.Persons.Include('Email').Include('Telefon')

您可能需要先在Personen AsQueryable()上调用DbSet才能获得Include扩展方法。

EF默认不检索电子邮件和Telefon的原因是,它认为这些不是归Person所“拥有”,而是与Person有关系的独立实体,您可能并不总是对检索所有这些相关实体感兴趣。

相反,您可以让EF知道这些在逻辑上是Person的一部分,应将其作为一个单元进行处理。您可以通过从电子邮件和Telefon中删除“人员”导航属性并在模型构建器中使用OwnsMany()来实现。