Seed Many-to-Many Data in Entity Framework - ef-code-first

I am using code first and have a many-to-many relationship between Book Titles and Categories. What is the best way to seed the data during development? If I add two books in the same category, the Seed logic adds the category twice to the category table. I can add categories separately to the Category table, but then how do I specify that existing category records in the books collection of keywords.

I credit this blog entry with showing me the way http://blog.goranobradovic.com/2011/06/asp-net-mvc3-app-part-1-entity-framework-and-code-first/comment-page-1/#comment-1663
The key is to establish the category objects separately from the book creation and then use those objects when creating the ICollection
var catCSharp = new Category {Name="CSharp"};
var catEF = new Category {Name="Entity Framework"};
var Categories = new List<Category> () {catCSharp, catEF};
var Books = new List<Book>();
Books.Add(new Book {Title="Entity Framework",
Categories=new List<Category>() {catCSharp, catEF}};
Books.ForEach(b => context.Books.Add(b));
Even though I only populate the context.Recipes DbSet, the Categories table gets populated and CategoryRecipes table, both get populated correctly.
Thank you Goran Obradovic

Related

vue + firestore: how to bind / merge data with vuefire?

I have a cloud firestore with data structured this way:
baskets
basket 1
basketID
userID
description
products
productID
productID
basket 2
basketID
userID
description
products
productID
productID
...
basket N
products
product A
productID
description
productImg
price
product B
productID
description
productImg
price
...
product Z
This structure in order to avoid duplicating detailed product data inside each basket document.
I created a vue component to display at once both the basket data and the products data associated to the basket, while trying to limit and optimize firebase queries. I have sucessfully binded the basket data like this:
data() {
return {
basket: [],
basket_products: []
};
},
created() {
this.$bind('basket', db.collection("baskets").where("basketID", "==", this.basketID))
}
However I'm struggling to bind the basket_products data for the current basket. I'm looking for something like this:
this.$bind('basket_products', db.collection("products").where("productID", 'in', ONE OF THE PRODUCT_IDS WITHIN CURRENT BASKET, EQUIVALENT TO this.basket[0].products))
Glad my pointer helped!
Just for completeness sake - the best solution as mentioned is to simply store references, vuefire automatically picks up on those and saves them accordingly as part of the baskets thanks to the maxRefsDepth option.
Aside from that, you could potentially play around with the serialize option of vuefire to modify (and query additional) data when getting the baskets.
But this would be fairly complex, you might be better off just manually subscribing using the firebase SDK instead.

Best way to create a custom lookup

I would like to know what the best way is to create a custom lookup for a field in my table, my situation is as following:
I have Form A which has a datasource to Table A, a field on that datasource has a lookup method:
public void lookup(FormControl _formControl, str _filterStr)
{
changeCompany(companyInfo.DataArea)
{
super(_formControl, _filterStr);
}
}
The field has an EDT, which has an relation to a Table.
The table has multiple fields, 1 of them is field: GroupType (Enum), with 2 options: Suppliers and Customers.
Form A is showing all records, both with Suppliers and Customers, but i would like to filter on the records with only has the value Suppliers in Column C.
Based on the information above, what is the best way to create this custom lookup?
You can create Related field fixed relation between your tables
TableB = TableB.Id
Enum::Suppliers = TableB.GroupType
Or create a custom lookup
and set a range for field GroupType.

$firebaseArray join with other table

I have a Firebase table named Orders:
{productID: xyz,
userID: abc,
quantity: 100}
There is another table Products containing product details. When showing a user's orders, I need to show some product details along with each order. How can I use $firebaseArray for this purpose?
$firebaseArray seems can only go with a table. Here, I'd need to query each ordered product's detail and add the detail to each order. Is the $extend the way to go? If so, any good example would be very appreciated.
Just saw this one. What you want to use is the NormalizedCollection. This way you can write your statement something like this:
var fb = new Firebase('https://<instance>.firebaseio.com');
var norm = new Firebase.util.NormalizedCollection(
fb.child('products'),
fb.child('productDetails')
);
var productsRef = $firebaseArray(norm.ref());

Orchard CMS Projection Query String With Taxonomies

I have created a Content Type called "Products" which has a few fields, 2 of which are Taxonomy Fields (Product Type and Material)
Product Types: Cable cleats, Cable clamps, Pole cleats, Cable core
Material: Stainless steel, Aluminium, Galvanised steel
I have a product listing and would like to filter it using the query string, over both taxonomies for example
List all products with product type of Cable cleats and material of Stainless steel
~/products?product-type[]=Cable cleats&material[]=Stainless steel
List all products with product type of Cable cleats or Pole cleats and material of Stainless steel
~/products?product-type[]=Cable cleats&product-type[]=Pole cleats&material[]=Stainless steel
(guessing it will be IDs used not the full text string for all queries)
Is there a way in Orchard to do this? or would it need a custom Module?
Any help would be much appreciated
Many thanks
Anto
Many projection filters have parameters where you can use tokens. To use the query string, you would use the QueryString token, like this: {QueryString:product-type}. Unfortunately, the taxonomy term filter does not currently work with tokens. It shouldn't be too hard to add that possibility however. Most of the code is already there.
Quite an old question, but because it was referenced here, i give here a copy of my answer...
Note: Tried with a recent Orchard dev branch.
Here, for testing, i've done some changes directly in TermsFilter.cs and TermsFilterForms.cs, but based on this example, you will be able to write your own IFilterProvider and IFormProvider...
So, in TermsFilterForms.cs, in the Describe() method where the form is defined, try to add this:
...
),
_ProductType: Shape.TextBox(
Id: "product-type", Name: "ProductType",
Title: T("Product Type"),
Classes: new[] { "text medium", "tokenized" },
Description: T("Enter the product type.")
)
);
...
Then, when editing your filter, you will see a new input field that can be tokenized and where e.g you can put:
{Request.QueryString:product-type}
Then, in TermsFilter.cs, you can inject a tokenizer:
...
private readonly ITokenizer _tokenizer;
public TermsFilter(ITaxonomyService taxonomyService, ITokenizer tokenizer) {
_taxonomyService = taxonomyService;
T = NullLocalizer.Instance;
_tokenizer = tokenizer;
}
...
Then in the ApplyFilter(dynamic contex) method, you can tokenize your product-type field like this:
var termName = _tokenizer.Replace((string)context.State.ProductType, null);
Here, for testing, in the query string product-type parameter, i expect only one value (not an array) that is a term name (not an id). But you can change the code according to your needs...
Then, by using the taxonomy service, you can do things like that:
var taxoPart = _taxonomyService.GetTaxonomyByName("Product Type");
var termPart = _taxonomyService.GetTermByName(taxoPart.Id, termName);
And then you can use e.g the termPart.Id to update the context.Query (see in the code)...
Best

How do I deal with the quantity column of a junction table using sql to entities

I have 3 tables. Blog and Tag have a many to many relationship. BlogTag is a junction table with a quantity column.
**Blog**
BlogID
Title
**Tag**
TagID
Name
**BlogTag**
BlogID
TagID
Quantity
I'm not sure how I handle the quantity column. I'd like it to store how many Blogs have a certain Tag Name
How do I deal with the quantity column when adding a new blog that has tags?
Thanks!
Well, you'd need to calculate the quantity:
var q = (from b in Context.Blogs
where b.BlogTags.Any(t => t.Tag.TagId == someId)
select b).Count();
So you:
1. Add the blog
2. SaveChanges
3. For each tag on the new blog:
1. Calculate the quantity, as above.
2. Update the BlogTag.Quantity.
4. SaveChanges

Resources