Код: Выделить всё
public partial class Contact
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Contact()
{
Contact_Category_Pivot = new HashSet();
}
[Key]
[Column("Contact ID")]
public int Contact_ID { get; set; }
[Column("Contact Name")]
[StringLength(255)]
public string Contact_Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection Contact_Category_Pivot { get; set; }
}
< /code>
Contact_Category
Код: Выделить всё
public partial class Contact_Category
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Contact_Category()
{
Contact_Category_Pivot = new HashSet();
}
[Key]
public Guid Contact_Category_ID { get; set; }
[Required]
[StringLength(50)]
public string Contact_Category_Name { get; set; }
[Required]
[StringLength(100)]
public string Contact_Category_Description { get; set; }
public int Sequence { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection Contact_Category_Pivot { get; set;
}
< /code>
Contact_Category_Pivot
Код: Выделить всё
public partial class Contact_Category_Pivot
{
[Column("ID")]
[Key]
public Guid PivotID { get; set; }
[Column("Contact_ID")]
public int Contact_ID { get; set; }
public Guid Contact_Category_ID { get; set; }
public Contact Contact { get; set; }
public Contact_Category Contact_Category { get; set; }
}
< /code>
OnModelCreating
Код: Выделить всё
modelBuilder.Entity()
.HasMany(e => e.Contact_Category_Pivot)
.WithRequired(e => e.Contact)
.HasForeignKey(e => e.Contact_ID);
modelBuilder.Entity()
.HasMany(e => e.Contact_Category_Pivot)
.WithRequired(e => e.Contact_Category)
.WillCascadeOnDelete(false);
< /code>
In case it is unclear; the pivot table holds a key from each other table plus an ID per record (RecordID, ContactID, ContactCategoryID
Если я пропустил какие -либо необходимые образцы, дайте мне знать.
Ошибка возникает при попытке найти «Контактная категория» с использованием категории , полученной из таблицы Pivot (для целей устранения неполадок я здесь жестко кодировал идентификатор):
Код: Выделить всё
Contact_Category CG = db.Contact_Category.Find(new Guid("a2ab17df-5a15-4b38-aa5f-4577653a1ec9"));
< /code>
On that line, I get an error
Invalid column name error on Contact_Contact_ID
I know the problem is related to the foreign key relationship, I'm just not sure what I need to do to resolve it.
I've tried various changes based on answers I've seen but each seems to create a new problem, so I've had to resort to requesting assistance from all you wonderful folk! Hopefully someone spots something obvious to those more experienced with code-first EF!
[b]Note[/b]: I believe the supplied samples are all auto-generated but it is possible changes from my tinkering have slipped in
[b]Bonus[/b]: once I have this sorted, I need to figure out how to do the initial-migration step accounting for these changes, so if anyone can point me in the right direction for that if its likely to be an issue, that would also be appreciated
[b]EDIT[/b]
I think I might know where the problem is coming from? But I am still unsure how to fix it.
The Contact_Category_Pivot
позже примечание: я пытались изменить имя столбца в базе данных, но это не помогло, так что, возможно, нет?
Подробнее здесь: https://stackoverflow.com/questions/794 ... -codefirst