I have a simple object model as follows...
public class Product
{
public long ProductId { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
and
public class Category
{
public long CategoryId { get; set; }
public List<Product> Products { get; set; }
}
Generating the underlying database with EntityFramework results in the following schema...
Products
- ProductId
- CategoryId
- Category_CategoryId
Categories
- CategoryId
In the Products table, the CategoryId column is always set to 0 while the Category_CategoryId column contains the id of the category the product belongs to.
How do I cause the category id to be set in the CategoryId column and prevent the Category_CategoryId column from being generated?
Apply ForeignKey attribute to
Category
ORCategoryId
property. And changeCategoryId
property type to matchCategoryId
ofCategory
class (both should be long or int).OR
You can do same via fluent mapping: