EF懒惰装载流畅的API在表中创建了额外的字段 [英] EF lazy loading & fluent API creates additional field in table

查看:131
本文介绍了EF懒惰装载流畅的API在表中创建了额外的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码 - 首先,附件 class:

Here is my code - first, Attachment class:

    public class Attachment{
    (...)
          public int UserId { get; set; }
          public virtual User Author { get; set; }

          public int? BasicMedicalDataId { get; set; }
          public virtual BasicMedicalData MedicalData { get; set; }
    }

这里是 BasicMedicalData class:

    public class BasicMedicalData{
    (..)
          public virtual ICollection<Attachment> Attachments { get; set; }
    }

如您所见,附件可以有一个连接的 BasicMedicalData 对象。
BasicMedicalData 可以有很多附件对象。

As you can see, Attachment can have optionally one connected BasicMedicalData object. BasicMedicalData can have a lot of Attachments objects.

这些行在

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        (..)
        modelBuilder.Entity<Attachment>()
            .HasOptional(u => u.MedicalData)
            .WithMany()
            .HasForeignKey(u => u.BasicMedicalDataId);


        modelBuilder.Entity<Attachment>()
            .HasRequired(a => a.Author)
            .WithMany()
            .HasForeignKey(a => a.UserId);

    }

它创建了奇怪的迁移:

    public override void Up()
    {
        CreateTable(
            "dbo.Attachments",
            c => new
                {
                    AttachmentId = c.Int(nullable: false, identity: true),
                    name = c.String(),
                    UserId = c.Int(nullable: false),
                    AddDate = c.DateTime(nullable: false),
                    BasicMedicalDataId = c.Int(),
                    del = c.Boolean(nullable: false),
                    BasicMedicalData_BasicMedicalDataId = c.Int(),
                })
            .PrimaryKey(t => t.AttachmentId)
            .ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
            .ForeignKey("dbo.PC_BasicMedicalData", t => t.BasicMedicalData_BasicMedicalDataId)
            .ForeignKey("dbo.PC_BasicMedicalData", t => t.BasicMedicalDataId)
            .Index(t => t.UserId)
            .Index(t => t.BasicMedicalData_BasicMedicalDataId)
            .Index(t => t.BasicMedicalDataId);
    }

问题在这里 - 它在数据库中创建额外的字段 BasicMedicalData_BasicMedicalDataId 并忽略加载字段 BasicMedicalDataId

Problem is here - it creates additional field in database BasicMedicalData_BasicMedicalDataId and ignores in lazy loading with field BasicMedicalDataId.

当我将手动ID放在 BasicMedicalData_BasicMedicalDataId 中时,使用附件的延迟加载工作很好。但是我不能把这些代码放在这里,因为它不是一个附件属性..

When I put manually ID's in BasicMedicalData_BasicMedicalDataId, lazy loading with attachments works nice. But I cannot put here any value with code, because it is not a Attachments property..

如何解决它?为什么它使用不同名称创建2x相同的字段?为什么懒惰加载字段 BasicMedicalDataId 不能正常工作?

How to resolve it? Why it creates 2x the same field with diffrent name? And why lazy loading with field BasicMedicalDataId does not working well?

推荐答案

您已经使用配置关系为可选的 WithMany()重载:许多在关系的另一边没有导航属性 - 但是您有一个导航属性关系的另一面。

You have used the WithMany() overload that configures the relationship to be optional:many without a navigation property on the other side of the relationship - but you do have a navigation property on the other side of the relationship.

尝试这样:

   modelBuilder.Entity<Attachment>()
      //Attachment has an optional relationship with MedicalData
      .HasOptional(u => u.MedicalData)
       //It is many:optional with a navigation property named Attachments  
      .WithMany(d => d.Attachments)
      //The dependent's foreign key is BasicMedicalDataId    
      .HasForeignKey(u => u.BasicMedicalDataId);

参考:
可选WithMany方法

这篇关于EF懒惰装载流畅的API在表中创建了额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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