在 Entity Framework 中配置多对多关系时遇到问题

Trouble configuring many to many relationship in Entity Framework

我正在尝试使用代码优先 EF6 从数据库复制 M-M 关系,但没有成功。我尝试了一些教程链接和此处的一些答案,但仍然出现相同的错误。

这只是我尝试过的一些东西:
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx
relationship problems in EF code-first

错误:实体类型 'AdjustmentType' 和 'AdjustmentReason' 无法共享 table 'AdjustmentReason' 因为它们不在同一类型层次结构中或不具有有效的一对一外键关系,并且它们之间具有匹配的主键。

谢谢!

[Serializable]
[Table("schema.AdjustmentReason")]
public class AdjustmentType : AuditableEntity
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AdjustmentTypeId { get; set; }

    [Column("AdjustmentType")]
    [StringLength(50)]
    public string AdjustmentType1 { get; set; }

    public virtual ICollection<AdjustmentReasonType> AdjustmentReasonType { get; set; }
}

[Serializable]
[Table("schema.AdjustmentReason")]
public class AdjustmentReason : AuditableEntity
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AdjustmentReasonId { get; set; }

    [Column("AdjustmentReason")]
    [StringLength(50)]
    public string AdjustmentReason1 { get; set; }

    public bool? Hidden { get; set; }

    public ICollection<Transaction> Transactions { get; set; }
    public virtual ICollection<AdjustmentReasonType> AdjustmentReasonType { get; set; }      
}

public class AdjustmentReasonType : AuditableEntity
{
    public int AdjustmentReasonTypeId { get; set; }

    public int AdjustmentReasonId { get; set; } //This is required

    public int AdjustmentTypeId { get; set; } //This is optional

    public virtual AdjustmentType AdjustmentType { get; set; }

    public virtual AdjustmentReason AdjustmentReason { get; set; }

}

//DatabaseContext.cs
        modelBuilder.Entity<AdjustmentReason>()
            .HasMany(e => e.AdjustmentReasonTypes)
            .WithRequired(e => e.AdjustmentReason);
        modelBuilder.Entity<AdjustmentType>()
            .HasMany(e => e.AdjustmentReasonType)
            .WithRequired(e => e.AdjustmentType);
//IDatabaseContext.cs
        IDbSet<AdjustmentReason> AdjustmentReasons { get; set; }

        IDbSet<AdjustmentType> AdjustmentTypes { get; set; }

        IDbSet<AdjustmentReasonType> AdjustmentReasonTypes { get; set; }

一些可能有助于...的改动

public class AdjustmentType : AuditableEntity
{ //Good...
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AdjustmentTypeId { get; set; }

    [Column("AdjustmentType")]
    [StringLength(50)]
    public string AdjustmentType1 { get; set; }

    public virtual ICollection<AdjustmentReasonType> AdjustmentReasonType { get; set; }
}

public class AdjustmentReason : AuditableEntity
{ //Good...
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int AdjustmentReasonId { get; set; }

    [Column("AdjustmentReason")]
    [StringLength(50)]
    public string AdjustmentReason1 { get; set; }

    public bool? Hidden { get; set; }

    public ICollection<Transaction> Transactions { get; set; }
    public virtual ICollection<AdjustmentReasonType> AdjustmentReasonType { get; set; }      
}

public class AdjustmentReasonType : AuditableEntity
{ // Remove the FKs...
    public int AdjustmentReasonTypeId { get; set; }

    //public int AdjustmentReasonId { get; set; } //This is required

    //public int AdjustmentTypeId { get; set; } //This is optional

    public virtual AdjustmentType AdjustmentType { get; set; }

    public virtual AdjustmentReason AdjustmentReason { get; set; }

}

//DatabaseContext.cs
// Add the FK declarations here so that EF knows how to resolve these back to the parent references.

        modelBuilder.Entity<AdjustmentReason>()
            .HasMany(e => e.AdjustmentReasonTypes)
            .WithRequired(e => e.AdjustmentReason)
            .Map(e=> e.MapKey("AdjustmentReasonId"); // FK column name.
        modelBuilder.Entity<AdjustmentType>()
            .HasMany(e => e.AdjustmentReasonType)
            .WithRequired(e => e.AdjustmentType)
            .Map(e=> e.MapKey("AdjustmentTypeId");

//IDatabaseContext.cs
        IDbSet<AdjustmentReason> AdjustmentReasons { get; set; }

        IDbSet<AdjustmentType> AdjustmentTypes { get; set; }
        // Remove these, typically you would be dealing with Reasons or Types, not the linking table directly. Use the references
        //IDbSet<AdjustmentReasonType> AdjustmentReasonTypes { get; set; }

找到错误的解决方法!
原来,我在 class 的数据注释中将 AdjustmentReason 和 AdjustmenType 都命名为 "Schema.AdjustmentReason"。我已经更新了问题区域中的代码以显示我在 classes 中遇到的数据注释错误。

之前:

[Serializable]
[Table("schema.AdjustmentReason")]
public class AdjustmentType : AuditableEntity

[Serializable]
[Table("schema.AdjustmentReason")]
public class AdjustmentReason : AuditableEntity

之后:

[Serializable]
[Table("schema.AdjustmentType")]
public class AdjustmentType : AuditableEntity

[Serializable]
[Table("schema.AdjustmentReason")]
public class AdjustmentReason : AuditableEntity

这是帮助我意识到我做错了什么的 link: http://geekswithblogs.net/tonyt/archive/2013/07/02/153327.aspx

很抱歉之前没有放数据注释,我认为不需要它,为此我很抱歉。

谢谢!