我尝试使用代码优先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'不能分享表' 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; }
答案 0 :(得分:1)
可能有所帮助的一些改动...
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; }
答案 1 :(得分:0)
找到错误的解决方案!
原来我在类的数据注释中将AdjustmentReason和AdjustmenType命名为"Schema.AdjustmentReason"
。我更新了问题区域中的代码,以显示我在两个类中都有的数据注释错误。
在:
[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
这是帮助我意识到自己做错了什么的链接:
http://geekswithblogs.net/tonyt/archive/2013/07/02/153327.aspx
我为之前没有提供数据注释而道歉,我不认为这是必要的,因为我很抱歉。
谢谢!