Entity Framework code first add unneeded foreign key column

I have many relationships between Categories and News .

The problem I'm running into is that EF keeps adding a foreign key table to the table, which I don't want!

News class

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; }
}

      

Category class

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; }
}

      

Database

Database

My question

How to remove Category_CategoryID in the News table ?

I am guessing that I am missing the code in the OnModelCreating method.

+3


source to share


1 answer


You need to add an id field to the class News

for the Category link.



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; }
}

      

+2


source







All Articles