context.AddObject("User",myuserobjetc)
This add a user to the User table.
how can i insert a list of users to user table(with out looping)?
entity-framework-4
I'm afraid you can not do bulk insert with EF4. Here is another topic about that.
Related
How can I Add data to a many-to-many relationship using Entity Framework?
I have got a problem when I try to add one article with its related categories (see diagram), but the thing is that the categories already exist in the Categories Table, i don't need to create them again. When I use EF Method: _ctx.Articles.Add(newArticle), It generates insert sentences to categories table and duplicate my rows. Basically, I only need to add data to Article table and the reference table CategoryArticles. Add method, add rows to Article table (ok) plus Category table (wrong step) plus CategoryArticles table (ok).Any Idea How can I avoid to add these rows in my category table again.? Appreciate any help.
EDIT: I AM USING AUTOMAPER FOR FILLING AN ARRAY OF CATEGORIES... I DO NOT IF MAYBE DOING IT ON THAT WAY I AM CREATING NEW OBJECTS. IT IS THE CODE:
List<CategoriesViewModel> categoriesViewModel = article.CategoriesViewModel.Where(c => c.IsSelected).ToList();
List<Category> selectedCategories = Mapper.Map<List<CategoriesViewModel>, List<Category>>(categoriesViewModel);
newArticle.Categories = selectedCategories;
_ctx.Articles.Add(newArticle)
This is the relational model
Thanks to #Robert McKee, the way to solve the problem is to fetch the categories directly from the database, that way the entity framework automatically recognise these categories when the main entity is being saved, avoiding duplicate them.
Many to Many relationships hide the cross reference table in EF. The relationship model shouldn't show the CategoryArticles table at all. The Articles table should have a "Categories" navigation property. Just add a category to it, and save.
I have two tables which has "photo URL" columns, that contain same image URL. So when I change value in one table, I would like the value in another to update automatically. So how can I set this relationship in Backendless? Like foreign key in SQL.
EDIT:
I have included Users table as property of ActionCreation table. Users have property for URL of logo for the user. In ActionCreation table I need to have exactly the same photo URL. When I included Users as property of ActionCreation, there is no custom properties are loaded form Users object. But I need access photo URL in my app. What the best way to do it?
Thank you.
Is there a relationship between the tables?
How does the same value get into two tables? Do you write it there twice?
Mark
I have two Entities billing and BillingDetail
in billing I am adding more than one time and after that I am adding data to BillingDetail finally I am Saving the Database. but when i checked database in billing table, it was containg only last data(only one row that was last updated)
How I supposed to solve this problem?
foreach ()
{
billingRepository.Insert(billModel);
}
billingDetailRepository.Insert(billDetailModel);
dbContext.Save();
When an entity is inserted is will be in attached state. Therefor Entity Framework will know it already exists in the context an not insert it again.
You can detach the entity and then insert it again to have it multiple times. But beware, after an insert PK fields are auto mapped to the entity.
As an example:
I have two tables in firebird:
TB_CUSTOMER
IDCUSTOMER (autoincrement generator)
CUSTOMERNAME
TB_PHONE
IDPHONE
IDCUSTOMER (foreing key from TB_CUSTOMER)
PHONE
I have a registration form developed in Delphi. The table data TB_PHONE are handled using a dbgrid. I can not assign the value of the field IDCUSTOMER in TB_PHONE, because it was not generated by the Firebird generator. How can I make the relationship between the tables? I want to implement it without first saving the table data TB_CUSTOMER. I'm using datamodules with IBDAC.
Any sugest?
Before detail table can be inserted into, you should have PK-index over master-table updated and having proper master-ID in it. That means that some piece of code should insert master-record before inserting detail-record. Where this piece of code would be - is only limited by your fantasy.
Few arrangements include
insert the master-row in your application. Read the id of the row. Insert detail-row using this id.
read ID from then Generator, then insert both rows (master 1st) using the obtained ID
create a stored procedure, inserting both rows and returning ID (implementing #1 or #2 server-side)
use EXECUTE BLOCK - basically ad hoc anonymous SQL procedure. But that only is available in FB 2.x and except for not using namespace it is inferior to #3.
add BEFORE INSERT trigger onto detail table, searching for ID in master and adding one if not found. This would slow down all insert operations (even when master-ID already exists - that should be checked), would not be able to fill all other master columns but ID and is potentially dangerous due to hiding application logic problems. But still that can be implemented (though ugly and dirty method)
create master-join-detail VIEW and add INSERT trigger for it, propagating the new view-row into both master-table and details-table.
et cetera
I want to implement it without first saving the table data TB_CUSTOMER
There's your problem. You need the primary key from the master table before you can save the detail. That's just the way it works. But if what you want is to make sure that the values get saved together, you can do that as a transaction. In Firebird, you can do it like this:
Begin a transaction. Exactly how you do that depends on which DB library you're using to access your Firebird database.
Run an INSERT INTO ... RETURNING statement to insert the row into your master table and retrieve the generated value as a single operation.
Use the generated PK value to fill in the FK value on your detail table.
Insert the detail row.
Commit the transaction.
Let's say you have a one-to-many relationship between Users and Orders (where one user can have many orders). Is it possible to create a User object, add orders to it, and save it in one go? Or do you have to save the User object first so that an ID is generated before you can save the orders collection?
You can check Railscasts for that. Here's an example of a nested model - Nested Model Form Part 1
Since the User is a new record, orders will be saved automatically once the user is saved.
I was able to solve this by using the "build" method. From the ActiveRecord::Associations::ClassMethods documentation:
Returns one or more new objects of the
collection type that have been
instantiated with attributes and
linked to this object through a
foreign key, but have not yet been
saved.