2

I'd like to insert data into a database that is 1GB in size. I am currently querying the database with LINQ-to-Entities.

The problem is that the database is large - I don't want to have to pull the entire database into memory just to make an insert.

Whats your recommended method to insert data into a specific table, reasonably efficiently?

Update:

I should clarify that I'm inserting data into a table thats linked to two other tables with a one-to-many relationship. Is there a method of letting a the ADO framework handle the updating of primary/foreign key relationships?

Update:

Found the correct answer, see How do I use LINQ-to-Entities to insert data into a specific table?

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • 1
    Oh boy, you're in serious need of learning how databases and SQL work. Because the answer to this is "Just insert the data." – R. Martinho Fernandes Feb 07 '11 at 16:34
  • Not quite that simple when you have multiple tables that require syncing with primary/foreign keys - see http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx – Contango Feb 07 '11 at 20:29

4 Answers4

2

You can use Storage Procedures beacuse of the amount of data. Or you can create a file with the data, read it line per line, and insert line per line with Linq so the data is not loaded in memory at the same time

Why should you use stored procedures? Let's take a look at the key benefits of this technology: Precompiled execution. SQL Server compiles each stored procedure once and then reutilizes the execution plan. This results in tremendous performance boosts when stored procedures are called repeatedly. Reduced client/server traffic. If network bandwidth is a concern in your environment, you'll be happy to learn that stored procedures can reduce long SQL queries to a single line that is transmitted over the wire. Efficient reuse of code and programming abstraction. Stored procedures can be used by multiple users and client programs. If you utilize them in a planned manner, you'll find the development cycle takes less time. Enhanced security controls. You can grant users permission to execute a stored procedure independently of underlying table permissions. In MSSQL the language is Transact-SQL (T-SQL).

SebRojas
  • 198
  • 1
  • 3
  • 10
  • What are "Storage Procedures"? – Dean Kuga Feb 07 '11 at 17:53
  • Ups, Stored Procedures...The DB motors have a Programming language that permits to interact directly with data. See answer again – SebRojas Feb 07 '11 at 19:11
  • I'd rather no use stored procedures, because it will lock me into using MSSQL. I'd rather use something like LINQ to Entities, or ADO.NET so that I can use a different database in the future if I wish. As everything is running on the server, internet bandwidth is not a problem, and speed is not a problem either as we aren't dealing with much data. – Contango Feb 07 '11 at 20:22
1

You do not need to query anything to do an insert. When using the entity framework for doing an insert, LINQ will not play a role. Use the Create... factory method of the entity you want to create. Then, add it to the entity context and call the SaveChanges() method.

For example (vb.net), if working with a table / entity called Orders and an entity context called OrderEntities:

Using context As New OrderEntities
    Dim order As Order = Order.CreateOrder(OrderNumber, Description, etc)
    context.AddToOrders(order)
    context.SaveChanges()
End Using

This will be transformed into an INSERT statement to the database.

This is a greatly simplified example. You may need to add associations, perform validation, etc. However, it illustrates that you can insert using the entity framework without executing a LINQ to Entities query first.

EDIT:

Is this table on the one side or the many side of that relationship? If it is on the one side (the primary key table), you don't need to do anything because the foreign key reference is in the other table. If it is the many side (the foreign key table), then before you call the AddTo() method, you will need to set the foreign key association.

If the foreign key is an entity loaded into the entity context, you can set it directly: order.Customer = customer

However, if it is not loaded from the database, you can set the EntityKey: order.CustomerReference.EntityKey = New EntityKey("OrderEntities.Customer", "CustomerID", 5)

DCNYAM
  • 11,966
  • 8
  • 53
  • 70
  • I should clarify that the table I'm attempting to insert into is linked to two other tables with a one-to-many relationship. I have to update the primary and foreign keys in both tables to do an insert. Is there a way to let the framework handle this for us? – Contango Feb 07 '11 at 16:58
0

Yes.

using(Entities context = new Entities() {
{
 Entity1 e1 = new Entity1{Id=-1, Name="E1"});
 Entity2 e2 = new Entity2{Id=-1, Name="E2", Entity1=e1});
 context.Entities2.AddObject(e2);
 context.SaveChanges();
}

I'm not sure if you have to add e1 to the Entities1 collection.

In order to this works, you have to define in your tables de Id as Identity columns.

SebRojas
  • 198
  • 1
  • 3
  • 10
  • This is definitely getting closer - but is there a way to do it without having to worry about setting the primary keys and foreign keys yourself? – Contango Feb 07 '11 at 20:19
  • @Gravitas: If the primary keys are identity columns, auto increment columns, or auto-assigned through another mechanism, then you don't have to set them. For the foreign key, you have to establish the association within the entity framework. Without the association, the framework doesn't know which entities are related to which. If the primary key is auto-assigned by the database, the value will be cascaded to the related foreign key fields within the data model, but you still need the association for that to work. – DCNYAM Feb 07 '11 at 20:40
  • Thanks, appreciate the clarification. I have the answer now - see http://stackoverflow.com/questions/4926561/how-do-i-use-linq-to-entities-to-insert-data-into-a-specific-table – Contango Feb 07 '11 at 22:12
  • This is the generic answer...please do not leave the question open, if you see I did the aclaration about identity columns (autoincrement) – SebRojas Feb 08 '11 at 16:14
0

The correct answer to the question is under How do I use LINQ-to-Entities to insert data into a specific table?

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305