Custom properties in entity framework - entity-framework

I'm wondering if this is feasible and how. I've an entity that is a simple 1 to 1 mapping against a database. I'd like to add a custom property to this entity that is the result of a stored procedure call. This SP returns a collection of the same entity (hierarchical data).
Any ideas?
Thanks in advance,
Fabian

You could create a partial class with the same name and create a property that gets the collection. In the class have a private collection that you fill if empty in the get with a private function or just return it if the collection is already populated.

Related

EF and stored procedures to populate entries for an Entity

Is it possible to define an Entity that is not mapped to a table in database and to use a stored procedure to return the entries?
I found that I can use "Ignore" so the table in database is not created for an Entity, but how can I set a stored procedure to populate data for this entity?
Note: I am using code first.
Thanks.
You could create a normal model class that's not referenced by your database context. The model class should contain the properties you'll be returning from your stored proc. Then use
context.Database.ExecuteSqlCommand("storedProc", params)
// OR
context.Database.SqlQuery<YourEntityType>("storedProc",params);

Entity Framework 4.1 adds a GUID to entity object returned from DB

I have a strange problem when using Entity Framework code first.
When I return an object with dbContext.Users.Where... I don't get the User defined in my model, but User_{GUID}.
Is there anyone who knows this phenomenon and can help?
Regards,
Ajit
If your dbContext.Users if of a type DbSet<User> then you would get a User-castable type instance if you query Users collection.
User_{GUID} looks like a dynamic proxy object to a User instance in your DbContext. Treat it as if it was a User instance.

How to model a n to m relation with an attribute in Entity framework without adding the extra table

I'm pretty new to the Entity framework and I'm modelling this simple structure:
With this model what I have is a Users class with a property UsersGroups (a collection of UserGroups objects).
I would like to have a Users class with a property like Groups with type Tuple or something like this (a new PriorizedGroup class, etc) that is much more related with the bussines.
Is this possible with the Entity framework?
Thanks in advance.
EDIT: If I were modeling the bussines objects I would create a User class with a Groups property that contained all the groups the user pertains with an extra property to store its priority (with a tuple, with an inherited class, as you wish). The thing is that I feel that the objects created by the Entity framework resemble the SQL structure, not the business structure.
Not directly. EF can map the relation only in the way you see it at the moment but you can add your custom behavior to your partial part of the entity. The simple way is something like
public partial class Users
{
public IEnumerable<PrioritizedGroup> Groups
{
get
{
return UserGroups.Select(ug => new PrioritizedGroup
{
Priority = ug.Priority,
Id = ug.Group.Id,
Name = ug.Group.Name,
Description = ug.Group.Description
})
.OrderBy(g => g.Priority);
}
}
}
To make this happen directly in EF you need some advanced mapping technique which will require you to modify EDMX source code directly (either DefiningQuery or QueryView) and it will make the entity read only (you will need stored procedures for modification).
To make the collection exposed on Users updatable you would probably need to use ObservableCollection and transfer all modifications triggered by ObservableCollection back to original UserGroups collection. Once you have something like that implemented you can hide original collection.

stored procedure mapping Entity Framework

We're using a Function Import in an EF4 model to populate an existing entity in our Model. The entity in the model has a Key field of Id which we're struggling to map as our stored procedure doesn't return an Id field. I've tried setting the value in the mapping to a literal value of 0 but that fails with an EntityCommandExecutionException and the following exception text.
The data reader is incompatible with the specified 'Candidate'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name.
Short of modifying the stored procedure to return a dummy Id field can anyone recommend what the best approach is for this as the dummy field option feels very clunky to me.
Many Thanks
If you can't return enough data to fully materialize the entity -- and the Id field is certainly going to be required for that -- then you need to change the return type on the proc to be a complex type instead of an entity.
Use another POCO class with the same structure to receive the results of the stored procedure call, here's an example:
string sp = string.Format("EXEC dbo.spComercialesAsociadosActivos {0}", idComercialPrincipal);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<InfoComercial>(sp);
In this case "InfoComercial" is a POCO class with the same structure as "Comercial", which is tied up to EF code first in the DBContext, then I used this independent class in the viewModel to create a disconnected "Comercial", it's not an ideal solution but will work fine until EF 5 comes with SP support.

Add relationship between entity and property of another entity in coredata

I have an entity with an attribute of type "string". I actually want it to be an array. So I created an another entity "array" and I want to add a many-one relationship between string and the array.
So my question is: Is it possible to add a relationship between an entity and an attribute of another entity?
Thanx in advance.
You can use observer pattern for achieving this.