Let's say I have two tables: table Category and table Book
I would like to add a Category, then use its ID for inserting a book (which has a CategoryId field pointing to the table Category).
To get this info, I now commit my changes after inserting the category as it is not provided otherwise.
Is there a way to point to this category when inserting my book without commiting after inserting the category?
Thanks in advance
If the ID is generated by the database, then no, you can't get it until it is committed
It sounds like you have a FK relationship setup. If that's the case, if you associate the Category and Book entities together - EF should be create the necessary FK's.
For example:
var Category = new Category();
Book.Category = Category;
context.Books.Add(Book);
context.SaveChanges();
Would create a book, and a category and depending on how your mapping is setup the proper fields should be set. Can you post your model if this is not the case?
Related
I am new to EF so please bear with me. I am using Entity Framework with an existing database in which the relationships are not defined.
Using EF, I am able to produce the model, but obviously the 'navigational properties' are not working. Is there a way I can specify the mapping between the entities?
For example, in my Product entity, I have a field CategoryID_fk (maps to Category entity). But since the relationships are not defined, I cannot load a Category while loading a Product entity.
Can someone guide me in this regard?
I do understand that it would be preferable to refactor our database but I am unable to do that now. Thanks in advance.
This link is very useful.http://www.entityframeworktutorial.net/
I think field names are not suitable conventions.Try to change field name as CategoryId or Category_CategoryId .I think it will work
Yes, you can do a group join. Basically you will retrieve Products and Category in separate queries and take an approach as indicated in this answer.
Something along these lines:
var productCategoryList = products.GroupJoin
(
categories,
p=> p.productId,
c=> c.CategoryId,
(p, c) => new ProductCategory
{
//Create your Product Category model here
}
).AsEnumerable();
so the story is very simple.
I have one table called Products and another Called categories. In addition, i have another table called ProductCategories that hold the relationship of catetories to their corresponding products (i.e, the table has two columns, ProductId, ColumnId).
For some reason, after adding all those table to my entity model, i don't have "Access" to it, hence i can do myentityModel.ProductCategories, so i could relational items between those two tables.
And yes, the ProductCategores table is added as "Association" to the entity model. i don't really understand that.
EDIT:
I do see that as part of creating new "Product" i can pass EntityCollection of "Category". So i do query from my entity model for a list of the matching categories that the user selected (on the webpage). so for example, i get (after query the model), an Objectset of "Category". However, i encountered two issues:
the 'AddObject' accept only EntityCollection, hence i need to re-create a set and then add all the objects from the ObjectSet to the entityCollection, in this process i need to detach it from the previous model and add it to the new collection. if not, i get an exception.
when i do the SaveChanges, i see that i get an exception that it was actually trying to Create new Category rather than adding new ProductCategory. again, am i missing something here?
Thanks.
This sounds like a Many-to-Many relationship. In your entity model, you don't need to declare the join table as a separate entity. Instead, you configure the relationship between the Products and the Categories as a Many-to-Many and add metadata about the join table. In Hibernate, you would have:
#ManyToMany(targetEntity=Categories.class, cascade={CascadeType.ALL}, fetch = FetchType.LAZY)
#JoinTable(name="tb_products_categories",
joinColumns=#JoinColumn(name="category_id"),
inverseJoinColumns=#JoinColumn(name="product_id")
)
#IndexColumn(name="join_id")
public List<Categories> getCategories() {
return categories;
}
When you query, the ORM layer takes care of determining SQL and traversing table joins.
OK, I have 3 tables, call them:
Person
PersonID
Name
Store
StoreID
Name
PersonStore
PersonID
StoreID
Now, I have a form which allows you to add stores to a person. However, I am getting the store ID back from the form. I don't really want to do a query to get the store object from Entity Framework. I just want to add to the table using the StoreID and the Person object which I have.
By default in EF this join table won't appear as an entity instead you'll get a many to many relationship which will show up as two navigation properties
i.e.
Person.Stores
Store.People
If you want to build a many to many relationship without retrieving the entities then attaching stub entities is the best way.
var person = // you already have the person
var store = new Store{StoreID = 5} // you know the storeID
ctx.AttachTo("Stores", store);
ctx.AttachTo("People", person); // assuming the person isn't already attached
person.Stores.Add(store);
ctx.SaveChanges();
The only problem with this code is it will fail if the relationship already exists, so you need to be sure you are creating a new relationship
For more on using Stub entities like this check out my post.
Hope this helps.
Alex
Edit from OP:
Since I am using EF4, I used the following code to remove the string from the attach (thanks to tip 13 from the link).
var person = // you already have the person
var store = new Store{StoreID = 5} // you know the storeID
ctx.Stores.Attach(store);
person.Stores.Add(store);
ctx.SaveChanges();
I have table with this structure ID PK and two columns with FK for example ActivityID and ContactID.
I try programmatically insert some value in this two FK columns.
How can I do this, any help is appriciated.Tnx
If you want to use your structure - you have to get instances of Activity and of Contact and just set corresponding properties on new entity.
var newActivityContact = new ActivityContact();// m_Entities.CreateActivityContact(0);
newActivityContact.Activity = activityRepository.GetById(activityId);
newActivityContact.Contact = contactRepository.GetById(contactId);
m_Entities.AddToActivityContact(newActivityContact);
m_Entities.SaveChanges();
I think the best solution would be to get rid of primary key, set up combination of ActivityID and ContactID as PK and then recreate whole model in visual designer. Every Activity object will have Contacts navigation property and every Contact will have Activities. You will be able to add contacts to activity by calling:
activity.Contacts.Add(contact);
If you really need additional ID, it will be more complicated.
i've got a database with a table named PropertyValues where i store every value i need to describe some properties of my database table rows.
For example, table Products which looks like this :
ID
OrderID //Products table is related with Order table
ProductName
ProductType_ID // ID of PropertyValues table which describes product type (food, parfume, chemicals)
ProductCountry_ID // ID of PropertyValues table which links to country where product came from
ProductStatusID //also ID of PropertyValues table which contains product status(availible, not availible)
with such database model, to get order and all it's products with their type, country and status i'll have to write something like this :
var orders = from o in dbEntities.Order.Include("Products.ProductType")
.Include("Products.ProductCountry")
.Include("Products.ProductStatus")
select o;
and the question is :)
can it be done automatically ( so all related entities will be included )
or maybe there is better approach ?
Thank You !
I think what you are looking for is either "Lazy Loading" or "Eager Loading" as Alex James pointed out.
This blog post explains that "Lazy Loading" is to be implemented in version 4.0 of the Entity Framework.
http://blogs.msdn.com/adonet/archive/2009/05/12/sneak-preview-deferred-loading-in-entity-framework-4-0.aspx
It can be done automatically, Google "Entity Framework lazy loading" to go about setting it up.