Can I map incomplete sproc columns to an entity? - entity-framework

I am using the latest beta of Visual Studio 2010 and the Entity Framework. It's mostly really neat, but here's my situation:
I have a table T with columns Id and Name.
I have an auto-generated entity with Id and Name properties.
Finally, I have a stored procedure that selects only Id from T.
Trying to map my entity to the stored procedure results in an EntityCommandExecutionException:
A member of [the entity], 'Name', does not have a corresponding column in the data reader with the same name.
That makes sense, but is there some way to partially populate my entity from the stored procedure call and then fully materialize it later with a second query?
Nine months ago, the answer to this question appeared to involve a great deal of manual labor. In my case, we have hundreds of stored procedures, and literally none of them return full rows. The Entity Framework has come a long way since then, so I am hoping something might've changed.
Thank you in advance for any help!

One approach might be to map the procedure results into a complex type, and then customize the code generation to add a method to this type which will materialize the entire object.
One possible hitch with this idea is that I'm not sure it's possible to customize code generation for complex types. You can certainly customize code generation for entity types, as explained in great detail in this post. It seems like you should be able to customize complex types, as well, but I've never tried it.

Related

Entity framework code first and stored proc

I apologize in advance as a I feel the answer to this question is out there, but I can't word my query properly, so I don't get much back.
Anyhow - I have a project where I mostly have a new data architecture aside from literally a couple of tables. I'd like to use EF code first. So my question is - is it possible to use code first on the majority of the project aside from a couple of classes for I which I can define stored procedures to go after tables that already exist?
Thank you
You can access the ObjectContext property of your DbContext instance in order to execute a stored procedure like so:
((IObjectContextAdapter)myDbContext).Objectcontext.ExecuteStoreQuery<ResponseType>(
"EXEC MyStoredProc #Value1, #Value2",
new SqlParameter("#Value1", value1),
new SqlParameter("#Value2", value2)
)
However, even if the tables exist, all you need is classes that match the tables - CodeFirst does not literally mean that the code has to exist first. As long as your classes match the table structure and name (or you use the fluent api or annotations to make them match), you'll be fine. Unless the stored procedures are doing something special that is hard to do in EF, I would stick to using EF consistently.

Passing a session variable to the table update stored procedure in Entity Framework

How do I pass a session variable to the Insert, Update, Delete stored procedures mapped to a Table in an Entity Framework, and how do I get the mapping in the EDMX diagram to work with it?
I have a database with Stored Procedures (SPs) defined for Inserting, Updating, and Deleting rows. The SPs expect a userid parameter to be passed to them for audit trail purposes, this parameter does not exist as a column in the table.
When I'm in the EDMX diagram specifying the mapping for the Parameter I don't see any way to either a) ignore the parameter or b) (preferred) set the value to a Session value. I've created a partial class mapping of the Stored Procedures, however the model doesn't seem to see it or even acknowledge that it is there. The whole premise behind using this technology is to let it do the work. I can't even tell it to ignore this parameter as it is optional and has a value assigned to it. I can't even edit the SP definition as the file is marked autogenerated, and will lose any modifications when it is regenerated.
Entity Framework seems like a well thought out approach, and I've looked at several tutorials and books on the subject. However, this seems like a really obvious thing to do. I've looked through stack overflow and the MSDN forums and found similar questions from years ago, but no one ever seems to answer the question, they just dance around the issue.

Get values in NotMapped property in model class Entity Framwork Code First using linq

I have the below scenario. I am using EF 5 Code first, MVC 4 on VS 2010. I am using the Unit of Work and Repository pattern for my project.
I am not sure if this is possible or not. Kindly suggest.
I have a model class representing a database table. In the model class, I have a property that is decorated as [NotMapped]. I have a Stored Proc that returns data, similar to the model class. However, when I get the data in a List from the SP, it does not contain value for the [NotMapped] column (SP returns data for the [NotMapped] column though). This may be logically correct with respect to EF.
All I want to know is, do we have a way to get data populated for the [NotMapped] column. I want to achieve, CRUD using LINQ (excluding R - Read).
I would recommend to create a separate complex type for the stored procedure results. Otherwise sooner or later you will find yourself writing code to distinguish between entities coming from the DbSet or from the stored procedure. When the come from the stored procedure they can't be used in joins, for example. Or checks whether or not the unmapped property is set.
A very dirty approach could be to have two different contexts. With code first it is possible to have different contexts with different mappings to the same types, with and without the column ignored (if you use fluent mapping, not with data annotations). But that only succeeds if you tell EF not to check the database schema, so using migrations is ruled out as well. I would not do it!! For the same reason as I mentioned above. I would hate to have a type with a property that sometimes is and sometimes isn't set.

A few basic questions about ADO.NET Entity Framework 4

I did work on ADO.NET Entity Framework v2 about two years ago. I have faint memories.
Incidentally, I happen to be working in a very secured (for want of a euphemism) environment where a lot of links are blocked and there's not much one can do. It does get in the way of studying and working, more than a bit.
Therefore, I have to rely on this forum for a few basic questions I have to get started again. This time, I am working on Entity Framework 4. Here are my questions.
All these questions relate to the EDM generated entities, i.e. not the Code First model.
1) Is my understanding correct? I can rename any column name in the EDM designer generated model and nothing breaks.
2) Foreign keys are expressed as navigational properties in the generated entity classes. No special consideration is required to maintain foreign key relationships. I recall in version 1 of EF, you had just ID properties and the navigational IQueryable/IEnumerable/EntityCollection/RelatedEnd properties were introduced in version 2. I just need to know if I need to do anything to retain/maintain foreign key relationships and referential data integrity. I assume I don't need to. A confirmation would be nice.
3) What are all the possible ways to execute a stored procedure? I recall -- one way to use ExecuteSQL or something on the Context.Database object, another to use EntityClient API and the third was to specify stored procedure names in the InsertCommand, SelectCommand, etc. thingies in the mapping window of the EDM designer. Is this correct?
4) How do you use those SelectCommand thingies in the mapping window on an entity? Do you just supply the names of the stored procedures or user defined SQL functions?
5) How do I create an entity out of a subset of a table? Do I just create an entity from the table and then delete the columns I don't need from the designer? What happens if there are required (NOT NULL in SQL) values that are not in the subset I choose?
6) How do I create a table out of a query or join of two or more tables.
1) You can rename columns in the designer and nothing should break (if the name is valid)
2) Navigation properties point to related entities. In EF4 foreign keys were added. Foreign key properties basically expose the database way of handling relations. However, they are helpful since you don't have to load related entities just to change relationship - you can just change the key value to the id of the other entity and save your changes
3) Yes. You can either execute the procedure directly - this is for stored procedures that are not related to CUD (Create/Update/Delete) operations. You can map CUD operations to stored procedure as opposed to having EF execute SQL statements it came up with for your CUD operations.
4) I think this is a bit out of scope - you probably can find a lot of blogs with images how to do it. Any decent book on EF should also have a chapter (or two) on this. Post a question to stackoverflow if you hit a specific problem.
5) Remove properties in the designer and then supply default value to the S-Space definition. I believe you cannot do this with the designer. You need to rightclick on the edmx file and open it with the Xml editor and manually edit it. Also see this: Issue in EF, mapping fragment,no default value and is not nullable
6) You can either create a view in the database or you can create entity set using E-SQL in your edmx file (I think this will be read only though) or you may be able to use Entity Splitting. Each of these is probably a big topic itself so I think you should find more about these and come back if you have more specific questions / problems

classes and data presentation

I hope someone can give me some guidance in how to best approach this situation.
I am using dbcontext, wpf and sql server.
I am having situations were the presentation of the data requires other data than just what is coming from a single table. For example, if I had a person table but wanted to show also how many books they had read from related data, say fields would be name, address, NoOfBooks.
I currently create a new class, called say PersonBookPM, that I fill up with data from a linq query which combines the two tables which includes the above three fields.I create an observablecollection of that and make that the itemssource of the grid/listbox.
When I am then adding data to that I then need to use the selecteditem, convert that back to the single entity of person, and attach it back in to the context.
It seems like the classes have already been defined by the code gen and I am repeating the process only slightly differently.
Am I going round the houses here?
Thanks Scott