I've been learning ADO.NET, and then EF Core. My assignment was to create a database application in C#, first in ADO.NET and then convert that app so it would use Entity Framework instead.
This was my design in ADO.NET, and it seemed to work fine.
CREATE TABLE Categories (
Id INT IDENTITY,
Name NVARCHAR(50) NOT NULL,
CONSTRAINT PK_Categories
PRIMARY KEY (Id)
)
CREATE TABLE CategoryCategories (
ParentCategoryId INT,
ChildCategoryId INT,
CONSTRAINT PK_CategoryCategories
PRIMARY KEY (ParentCategoryId, ChildCategoryId),
CONSTRAINT FK_CategoryCategories_ParentCategoryId
FOREIGN KEY (ParentCategoryId)
REFERENCES Categories (Id),
CONSTRAINT FK_CategoryCategories_ChildCategoryId
FOREIGN KEY (ChildCategoryId)
REFERENCES Categories (Id)
ON DELETE CASCADE
)
CREATE TABLE Products (
Id INT IDENTITY,
ArticleNumber NVARCHAR(50) NOT NULL UNIQUE,
Name NVARCHAR(50) NOT NULL,
Description NVARCHAR(500) NOT NULL,
Price DECIMAL(18,2) NOT NULL,
CONSTRAINT PK_Products
PRIMARY KEY (Id)
);
CREATE TABLE Products_Categories (
ProductId INT,
CategoryId INT NOT NULL,
CONSTRAINT PK_Products_Categories
PRIMARY KEY (ProductId, CategoryId),
CONSTRAINT FK_ProdCats_Prods
FOREIGN KEY (ProductId)
REFERENCES Products (Id),
CONSTRAINT FK_ProdCats_Cats
FOREIGN KEY (CategoryId)
REFERENCES Categories (Id)
ON DELETE CASCADE
);
The connection between Products and Categories - no problem, just an ICollection property in each of the two responding classes. Entity Framework immediately created a CategoryProducts junction table.
When creating a connection between an entity and itself, that's where I ran into a wall. I tried simply copying all the model building code for CategoryProducts generated by the migrations into an OnModelCreating method for CategoryCategories, and inserting the right names of the columns and the tables.
Didn't work, got complaints that CategoryCategories was in shadow mode. I've vacuum-cleaned the internet trying to find a solution, but I can't find any clear instructions. Is it really so much harder to do this in Entity Framework than in regular SQL, or ADO.NET? I thought Entity Framework was supposed to make things easier.
Any suggestions? If you need more information, please let me know.
Edit
Thought I might add the classes, for clarity's sake.
class Category
{
public int Id { get; protected set; }
[Required]
public string Name { get; protected set; }
public ICollection<Product> Products { get; protected set; }
// public ICollection<Category> ChildCategories { get; protected set; }
public Category(string name)
{
Name = name;
Products = new List<Product>();
// ChildCategories = new List<Category>();
}
}
class CategoryCategory
{
// EFC forced me to have an Id. Could not run Add-migr Initial without it
public int Id { get; protected set; }
[Required]
public int ParentCategoryId { get; protected set; }
[Required]
public int ChildCategoryId { get; protected set; }
public CategoryCategory(int parentCategoryId, int childCategoryId)
{
ParentCategoryId = parentCategoryId;
ChildCategoryId = childCategoryId;
}
}
class Product
{
public int Id { get; protected set; }
[Required]
public string ArticleNumber { get; protected set; }
[Required]
// EFC forcing me to have unprotected set
// Without it, I can not update articles :(
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public decimal Price { get; set; }
public ICollection<Category> Categories { get; protected set; }
public Product(string articleNumber, string name, string description, decimal price)
{
ArticleNumber = articleNumber;
Name = name;
Description = description;
Price = price;
Categories = new List<Category>();
}
public Product(int id, string articleNumber, string name, string description, decimal price)
: this(articleNumber, name, description, price)
{
Id = id;
}
}
With these classes, EF says The entity type 'CategoryCategories' is in shadow state. A valid model requires all entity types to have corresponding CLR type.
If I uncomment the ICollection ChildCategories part in Category, then EF creates the third column, CategoryId in CategoryCategories, which is very undesirable in my opinion. Why should I want the third ID referring to Categories, when I already have two of them?