Solved

ADO.Net Entity Framework: Map Store Procedure

Posted on 2011-02-24
9
594 Views
Last Modified: 2012-05-11
Hi guys!

Need some help with mapping a store procedure to this model..

I tried using TPT inheritence but with no luck.

If this is not possible, any other solutions?

(The store procedure I have takes values for all for all three tables.)
tables.JPG
0
Comment
Question by:ulf-jzl
  • 5
  • 4
9 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
It's not possible with the current version of EF to map a single stored procedure to multiple entities.
0
 

Author Comment

by:ulf-jzl
Comment Utility
If I use TPT and have one to one relationship it would work, the problem is the one to many relationship i have in the first two tables.

any tips how to change the architecture to solve this?
0
 

Author Comment

by:ulf-jzl
Comment Utility
it sould be solved by using the Entity Framework Extensions, but I can't figure out how to do that.
Any tips on that?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Not something i've used myself i'm afraid. From what i have read it supports pulling back multiple entities from a single procedure, but i haven't seen anything about going the other way.
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

by:ulf-jzl
Comment Utility
ok, do you have any tips how to solve it. leave EF4 for regular ADO.Net to solve this?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
You can do it easily enough with ADO.Net. You would need to manually create a parameter for each property of your enitites, but other than that its all straightforward stuff. Let su know if you need any help with it.
0
 

Author Comment

by:ulf-jzl
Comment Utility
something like this then?

Or can I use somehting form the model to make this better?

public static void InsertDeviceTestResults(string deviceId)
        {
            // create and open a connection 
            using (SqlConnection conn = new SqlConnection("Server=(local);DataBase=FBI.Data;Integrated Security=SSPI"))
            {
                // Open connection
                conn.Open();

                // Setup command
                SqlCommand cmd = new SqlCommand("InsertDeviceTestResults", conn) { CommandType = CommandType.StoredProcedure };

                // Add parameter to the stored procedure
                cmd.Parameters.Add(
                    new SqlParameter("@DeviceId", deviceId));

                // Execute the sproc
                cmd.ExecuteReader();
            }
        }

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Yes, only you'll need to repeat the "cmd.Parameters.Add" line for each property you need to pass to the SP.
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Except you will use "ExecuteNonQuery()" instead of "ExecuteReader()", if you don't return any data back.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now