Hierarchyid data type and Code First - c#

What I need to do, I have a comment table using the HierarchyID data type in sql server, and would like that mapped over to a Code First/EF 4.3 class. How can I achieve this?
Also how do I interact with the hierarchyID when inserting/deleting. Thank you very much.

This is a possible duplicate of HierarchyID in Entity Framework not working
EF does not support that data type http://thedatafarm.com/blog/data-access/sql-server-2008-data-types-and-entity-framework-4/

I'm not used to the data type, but I've carried out some investigations.
Seems like this type is supported only inside the SQL Server both natively and in hosted CLR. Getting it outside requires it to be converted to a string (see Data Type Conversion section here).
I think the only way is creating a view in the database converting the hierarchyid to nvarchar(4000). To make it updatable you can either define a set of insert/update/delete triggers for that view, or create a set of corresponding stored procedures and map them to the EF context. I don't remember if EF Code First is capable of using SPs for modification operations, but an updatable view should be fine since it looks like an ordinary table.
Hope this helps.

Related

call a stored procedure multiple select query in entity framework code first and then map it to model

I am working on a project using entity framework code first approach, I have a situation where I need to call a stored procedure which returns multiple table, hence I want to map the result to my model. please tell me if its possible to do it and if yes then how can i do it.
Code First currently only supports mapping to tables. This unfortunately means
that you can’t map Code First directly to stored procedures, views, or other database
objects. If you are letting Code First generate a database, there is no way to
create these artifacts in the database, other than manually adding them once Code
First has created the database. If you are mapping to an existing database, there
are some techniques you can use to get data from non-table database artifacts.
i am also facing the same problem and not able to get any solution, so i called stored procedure using ExecuteReader and then mapped it to models using autoMapper.
Let me know if you are looking for code

Executing custom sql including complex select joins using entity framework 4

My application currently uses ado.net to access the database.
It allows users to configure dashboards by passing custom sql. The custom sql includes joins on multiple tables and the columns of every table are included in the result.
We are migrating from ado.net to entity framework 4.
How do I execute the same queries using entity framework?
Also other code in the application requires firing custom complex join queries on the database. This is done by developers.
Yes you can use inline queries and even stored procedue in entityframework
see example for query http://msdn.microsoft.com/en-us/library/bb738451
see example for stored procedure http://msdn.microsoft.com/en-us/library/bb896334.aspx
If you have dynamic queries you cannot execute them through EF. EF works in strongly typed manner so it expects that you created the type with correct properties (with correct types) at design time (you can create the type at runtime as well but it requires you to create dynamic assembly, emit IL, etc.).
Use your old approach for this type of queries.

Entity Framework doesn't work with stored procedures!

Is it true that you cannot call an sp with the EF unless the sp returns an entity?
To test I created 3 Function Imports for an sp with 1. no return type 2. a scalar return type and 3. an entity return type
then when i type "DataContext" then "." I only get intellisense on the function that returns an entity!
I'm surprised this isn't a current feature!
What are people using as a workaround?
There is a workaround!
Julie Lerman wrote a post about this. Have a look at her blog: http://thedatafarm.com/blog/data-access/implement-select-stored-procedures-that-return-miscellaneous-data-in-ctp2-of-ef-designer/
It helped me a lot to implement my stored procedures.
Has to return an entity, yes.
Using powerful T4 templates
Entity Framework V1.0 feels and is an unfinished product set out to public too early. That's why it's possible to use T4 templates that create different code from EDMX files, that also supports scalar-type stored procedures.
We're using custom templates with lots of modifications so they create Business layer objects, interfaces for IoC/TDD as well as DAL and DAO. We get everything from EDMX files. Heck we even create enums but those are created from real data in the DB not EDMX files.
You'll be able to find lots of T4 templates...
Here's one that does scalar stored procedures. But you may want to get one that actually does POCO.
In EF v1 you can only map procedures which return entities. In EF v4 you can map procedure results to complex types, so most procedures can be used without returning entities.
Use Chrigl's answer (+1) as a workaround for v1.

create entities for a dynamically created database in SQL Server

I have an application where I create database's dynamically in a SQL Server using Server Management Objects through my application.
I want to use the Entity Framework to access this database when it has been created, is this possible? As I can not generate Entity classes from a database in VS. I do have the structure of the database of course.
So is it possible to create the Entity classes manually and is that a do-able task?
Yes, it's completely possible. You can even manipulate the generated code if you want.
What you might want to take a look is the EDMX XML specification.
In that file you specify the underlying database, views, functions, procedures and the like, as well as the desired objects. Take a look at MSDN in order to have more information.
Paulo is right for EF 1 (+1). For EF 4, I'd suggest using code-only modeling instead.

using entity framework/nhibernate with sql2008 geography datatype

I am using sql express 2008 and vs2008, writing in c#.
I have a db table with a Geography column in it, into which I need to put gps data I collected. When I tried creating an Entity-Framework mapping for this table, it just ignored the column with some warning about not being able to map such column types.
I then looked at nHibernate.Spatial project, but it seems like it only translates the Geometry types, not the Geography. No luck there.
I've been told I can use a view with casting the Geography to VarBinary, and then in the created entity class add another Property that deserializes the binary back into Geography. I guess that will work for reading the data from the db, but I also need to insert those rows into my db, and I can't add rows to the view.
Is there some other trick I can use in order to easily read and write Geography data from my db, in my c# code?
I don't know much about EF, but NHibernate allows you to extend it's type framework using IUserType. There's a bit about it on Ray Houston's Blog.
I've successfully used this to create a type for that uses XmlSerialization to an Xml field, based on Ayende's example
At this time Entity Framework v1 does not support SQL UDT - 2008 Spacial type is UDT.
You can store your long/lat data using WKB (byte[]/varbinary).
As a side note i did not notice any support for UDT during the Microsoft MIX09 for the next version (1.5 CTP). Perhaps i missed something.

Categories