我在类别和新闻之间存在多对一关系。
我遇到的问题是 EF 一直在我的表中添加一个我不想要的外键列!
新闻类
public class News
{
public News()
{
}
[Key]
public int NewsID { get; set; }
public int PublishedByID { get; set; }
public string PublishedByFullName { get; set; }
public string PublishedByEmail { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string PreviewText { get; set; }
public string BlobName { get; set; }
public virtual Category Category { get; set; }
}
类别类
public class Category
{
public Category()
{
News = new HashSet<News>();
}
[Key]
public int CategoryID { get; set; }
public string Name { get; set; }
public string CategoryTypeName { get; set; }
public virtual ICollection<News> News { get; set; }
}
数据库
我的问题
如何删除 News 表中的 Category_CategoryID?
我猜我在 OnModelCreating 方法中遗漏了一些代码。
最佳答案
需要在News
类中添加id字段来引用Category。
public class News
{
[Key]
public int NewsID { get; set; }
public int CategoryID { get; set; } // added
public int PublishedByID { get; set; }
public string PublishedByFullName { get; set; }
public string PublishedByEmail { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string PreviewText { get; set; }
public string BlobName { get; set; }
public virtual Category Category { get; set; }
}
关于entity-framework - Entity Framework 代码首先添加不需要的外键列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31103482/