Relationship Using Code first with Existing database - entity-framework-4.1

When defining a relationship between two types is it important to include a navigation property on both types, such as in the following example:
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public ICollection<Product> Products { get; set; }
}
Can I do without including the navigation property in Category ?

If you just want it infered by code first convention then yes you need both on either side. I'd also make the collection "virtual" to support lazy loading.
You can set it up using the fluent configuration when the model is built. It would be something like this
modelBuilder.Entity<Product>()
.HasMany(x => x.Category)

Related

How to ignore navigation properties in asp.net mvc web api

I'm using ASP.NET Web API with Entity Framework but i'm facing a problem while generating the JSON for the navigation property:
I have two tables; Product and Category.
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
when I generate the JSON for Product it generates the JSON category which is fine but inside the Category JSON there is another JSON pointing to JSON product so a huge JSON file is create i tried to solve this issue by removing virtual but every time i update the model i face the same problem. is there any way to solve?
If you are using Newtonsoft.Json, then you can simply apply the attribute [JsonIgnore] to the properties you wish to ignore.
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
[JsonIgnore]
public virtual ICollection<Product> Products { get; set; }
}
With this setup, whenever category is serialized to Json, it will ignore the Products collection

Working with a Model class that has a foreign/navigation key to itself

I am trying to develop a catalog project in ASP.NET MVC 3 and using EF Code first with an existing Database. There is a Categories table in my database that points to itself. For that, I have written the following model class. --"Correct me if the model is wrong"--
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public int? ParentCategoryID { get; set; }
public string CategoryDesc { get; set; }
[ForeignKey("ParentCategoryID")]
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Question : I am unable to understand as to how can i work with this class. While using and passing the following code to the view
var cat = dbStore.Categories.Include("ParentCategory").ToList().
I got this error : Object reference not set to an instance of an object. This is happening because the root category has null ParentCategoryID. Please tell me how will you work with this code or any resource that can help me understand working in such scenarios. Just any sort of code will be helpful that uses the above the model, like displaying a list or a menu or anything, just anything.
Usually what you do is travel from top level categories to bottom level categories. Inorder to do that first you need to define SubCategories collection in your class
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public int? ParentCategoryID { get; set; }
public string CategoryDesc { get; set; }
[ForeignKey("ParentCategoryID")]
public virtual Category ParentCategory { get; set; }
[InverseProperty("ParentCategory")]
public virtual ICollection<Category> SubCategories{ get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Then you retrieve the top level categories
var topCategories = dbStore.Categories
.Where(category => category.ParentCategoryID == null)
.Include(category => category.SubCategories).ToList();
After that you can traverse the hierachey
foreach(var topCategory in topCategories)
{
//use top category
foreach(var subCategory in topCategory.SubCategories)
{
}
}
If you do not have very many categories you can solve this by loading the whole collection of categories. I think EF will handle the fixup for you so all relations are properly populated.
As far as I know there are no SQL'ish databases/ORM's that can handle this scenario well. An approach I often use is to load the whole collection as I said above and then manually fix the relations. But I do think EF will do that for you.
Basically you should do:
var topCategories = dbStore.Categories.ToList().Where(category => category.ParentCategoryID == null);

Configuring EF code first without a foreign key

I have the following model:
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Catalog> Matches { get; set; }
}
public class Catalog
{
public long Id { get; set; }
public string Title { get; set; }
}
Using Entity Framework code first I configure this using:
public DbSet<Product> Products { get; set; }
public DbSet<Catalog> Catalogs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// not rules setup yet
}
Currently when EF creates my database it creates a nullable foreign key in the Catalogs table called Product_Id. Is there a way to configure EF to not create any foreign key in the Catalog table?
The catalog table contains imported items from a catalog that should have no relation to the Products table. At run time a search query will be fired for each product and the result will be added to the catalog collection of the product object.
For your purpose I would exclude the Matches collection from the model, either by data annotation...
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
[NotMapped]
public virtual ICollection<Catalog> Matches { get; set; }
}
...or in Fluent code:
modelBuilder.Entity<Product>()
.Ignore(p => p.Matches);

EF Code First Custom Collections

When creating code first collections can you implement a custom class that implements ICollection. The code below is conceptual not actual
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
//Want to Avoid This
public ICollection<Product> Products { get; set; }
//Use his instead of above
public ProductList ProductsInCategory {get;set;}
}
public class ProductsList :ICollection<Product>
{
public int DiscontinuedProductsCount
{
return internalList.Count();
}
//Icollection Methods Excluded
}
EF can indeed support any collection which inherits from ICollection. We create a deletable collection to support auto deletions and also create collections for child objects to keep the size of our main object smaller.

EF 4.1 CF Fluent API mapping problemo

I'm new to the fluent API. I have a legacy database which I can't alter at the moment. Simply, this is what I need to achieve:
public class ItemCategory
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<ItemCategory> ItemCategories { get; set; }
public virtual ICollection<Item> RelatedItems { get; set; }
}
Items can be in many categories, RelatedItems can be in different categories to the current Item (which may not have any related items), the existing join tables look like this:
ItemCategoriesItems (ID,ItemCategoryID,ItemID)
RelatedItemCategoriesItems (ID,ItemCategoriesItemsID,RelatedItemCategoriesItemsID)
Hopefully it's obvious that the related items join table above contains 2 foreign keys to the item categories join table - one pointing to the current item and the other to the related item. Currently my onModelCreating code has:
modelBuilder.Entity<ItemCategory>()
.HasMany(c => c.Items)
.WithMany(set => set.ItemCategories)
.Map(mc =>
{
mc.ToTable("ItemCategoriesItems","testdb");
mc.MapLeftKey("ItemCategoryID");
mc.MapRightKey("ItemID");
});
... which gets the categories/items working but I'm stuck on how to get the RelatedItems.
Any help greatly appreciated!