Generated Code - Entity set pluralization && Stored procedure issue

Posts   
 
    
sundar
User
Posts: 13
Joined: 17-Aug-2010
# Posted on: 17-Aug-2010 18:25:49   

using LLBLGEN 3.0 Entity Framework 4, SQL Server 2008

  1. We are having an issue with entity set pluralization.

In the edmx / context file, Conceptual model entity set names are not plurlaized. LLBL desginer generates following code in the context file for Employee Entity.

public ObjectSet<Employee> Employees
    {
        get { return _employee ?? (_employee = this.CreateObjectSet<Employee>("Employee")); }
    }

When we try to reflect on Object Context to get the entityset name and add the object to the context

       var entitySetProperty =
           DbContext.GetType().GetProperties()
               .Single(p => p.PropertyType.IsGenericType && typeof(IQueryable<>)
               .MakeGenericType(typeof(TEntity)).IsAssignableFrom(p.PropertyType));

    EntitySetName = entitySetProperty.Name;
    DbContext.AddObject(EntitySetName, entity);

AddObject method fails here with below error {System.InvalidOperationException: The EntitySet name '<ContainerName>.Employees' could not be found. at System.Data.Objects.ObjectContext.GetEntitySet(String entitySetName, String entityContainerName)

Issue here is generated Property Name(Employees) and EntitySetName(Employee) are different

In VS Studio Entity Framework designer, it will plurlaize the entity names so the above code will be generated like this

public ObjectSet<Employee> Employees { get { return _employee ?? (_employee = this.CreateObjectSet<Employee>("Employees")); } }

How to control the Pluralization of EntitySet names (EDMX file and ObjectContext) from LLBL designer??

2.Another issue we have is whenever we add a stored procedure to our Entity model, LLBL code genetaor adds following line to EDMX file under StorageModels

<EntitySet Name="StoreProcedureName" EntityType="Self.StoreProcedureName" Schema="dbo" />

There is no entity type defined for stored procedure.This ends up in compilation error.

In VS Studio Entity Framework designer, stored procedures are not added to EntitySet.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-Aug-2010 18:29:36   

sundar wrote:

using LLBLGEN 3.0 Entity Framework 4, SQL Server 2008

  1. We are having an issue with entity set pluralization.

In the edmx / context file, Conceptual model entity set names are not plurlaized. LLBL desginer generates following code in the context file for Employee Entity.

public ObjectSet<Employee> Employees
    {
        get { return _employee ?? (_employee = this.CreateObjectSet<Employee>("Employee")); }
    }

When we try to reflect on Object Context to get the entityset name and add the object to the context

       var entitySetProperty =
           DbContext.GetType().GetProperties()
               .Single(p => p.PropertyType.IsGenericType && typeof(IQueryable<>)
               .MakeGenericType(typeof(TEntity)).IsAssignableFrom(p.PropertyType));

    EntitySetName = entitySetProperty.Name;
    DbContext.AddObject(EntitySetName, entity);

AddObject method fails here with below error {System.InvalidOperationException: The EntitySet name '<ContainerName>.Employees' could not be found. at System.Data.Objects.ObjectContext.GetEntitySet(String entitySetName, String entityContainerName)

Issue here is generated Property Name(Employees) and EntitySetName(Employee) are different

In VS Studio Entity Framework designer, it will plurlaize the entity names so the above code will be generated like this

public ObjectSet<Employee> Employees { get { return _employee ?? (_employee = this.CreateObjectSet<Employee>("Employees")); } }

How to control the Pluralization of EntitySet names (EDMX file and ObjectContext) from LLBL designer??

2.Another issue we have is whenever we add a stored procedure to our Entity model, LLBL code genetaor adds following line to EDMX file under StorageModels

<EntitySet Name="StoreProcedureName" EntityType="Self.StoreProcedureName" Schema="dbo" />

There is no entity type defined for stored procedure.This ends up in compilation error.

In VS Studio Entity Framework designer, stored procedures are not added to EntitySet.

For both issues, we'll look into them and fix them if necessary. (I assume you're using the latest build?). A fix will not be ready today but will be available tomorrow (wednesday). If that's too late, please let us know.

Frans Bouma | Lead developer LLBLGen Pro
sundar
User
Posts: 13
Joined: 17-Aug-2010
# Posted on: 17-Aug-2010 18:33:57   

Thanks. Wednesday should be fine

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Aug-2010 10:25:58   

Your reflection method could be fixed to support the current situation (which we actually want to keep, as it would otherwise break applications which rely on the singular name)

The property you obtain is returning an ObjectSet<T>, which allows you to obtain the real entity set name. This is more reliable, as it doesn't rely on the unwritten rule that the property name is equal to the entityset name:


static void Main(string[] args)
{
    var employee = new Employee();
    Console.WriteLine(GetEntitySetName(employee));
}


public static string GetEntitySetName<T>(T entity)
    where T:class
{
    var context = new STETesterEntities();
    var entitySetProperty = context.GetType().GetProperties()
                                        .Single(p => p.PropertyType.IsGenericType && typeof(IQueryable<>)
                                        .MakeGenericType(typeof(T)).IsAssignableFrom(p.PropertyType));

    return ((ObjectSet<T>)entitySetProperty.GetValue(context, null)).EntitySet.Name;
}

I hardcoded the datacontext class name in the small test code above, but you get the idea. You can cache these in a static dictionary to re-use them, so the performance hit will be minimal.

Another way, but less appealing is the option to generate the Add* methods to the context, but I can imagine you don't want that (it's an option in the project properties -> output setting values section)

Will now look into the proc problem

(edit) reproduced. very strange, seems like it was introduced with a recent change we made when we added support for entity to view mapping.

(edit) fixed in next build (08182010, released later today (wednesday))

Frans Bouma | Lead developer LLBLGen Pro
sundar
User
Posts: 13
Joined: 17-Aug-2010
# Posted on: 18-Aug-2010 17:54:45   

Thank you. That works. I will wait for stored procedure fix.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Aug-2010 19:11:20   

It's now available. Also an issue with EF entity splitting (2 entities on single table) has been resolved.

Frans Bouma | Lead developer LLBLGen Pro
sundar
User
Posts: 13
Joined: 17-Aug-2010
# Posted on: 18-Aug-2010 19:39:55   

Thanks. We are facing one more issue.

We tried to create a typed view from a sql servr view using "Reverse engineer to TypedView Definitions". In this case Sqlview is returning result sets . Designer displays following validation error "a typed view mapped onto targets of type "view" aren't supported as the Entity framework can't deal with types without a PK, multiple types mapped onto the same target" There is no way in the designer to mention whats the primary key on the Typed View. Please advice

Sundar

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Aug-2010 20:20:39   

sundar wrote:

Thanks. We are facing one more issue.

We tried to create a typed view from a sql servr view using "Reverse engineer to TypedView Definitions". In this case Sqlview is returning result sets . Designer displays following validation error "a typed view mapped onto targets of type "view" aren't supported as the Entity framework can't deal with types without a PK, multiple types mapped onto the same target" There is no way in the designer to mention whats the primary key on the Typed View. Please advice

Sundar

EF requires that each mapping target has a PK, both in the class and in the table/view. The EF designer 'cheats' by specifying all fields which are not nullable as the PK, but that's not always possible (nor is it correct). As a 'typedview' doesn't have a set of identifying fields, we can't mark fields as the PK for the view in the EDMX.

To work around this, map an entity onto the view instead and specify at least 1 field as the identifying field.

Frans Bouma | Lead developer LLBLGen Pro
sundar
User
Posts: 13
Joined: 17-Aug-2010
# Posted on: 18-Aug-2010 20:23:12   

Yes thats exactly what we tried here. BTW the new build worked for Stored procedure and Split Entity scenario. Thanks again for the quick turnaround.

Sundar

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Aug-2010 22:01:34   

sundar wrote:

Yes thats exactly what we tried here.

You could use this workaround or did it gave problems?

BTW the new build worked for Stored procedure and Split Entity scenario. Thanks again for the quick turnaround. Sundar

simple_smile

Frans Bouma | Lead developer LLBLGen Pro
sundar
User
Posts: 13
Joined: 17-Aug-2010
# Posted on: 18-Aug-2010 22:55:23   

it worked. But we need to create a composite primary key . Ugly the way entity framework works

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 19-Aug-2010 10:32:50   

sundar wrote:

it worked. But we need to create a composite primary key . Ugly the way entity framework works

Yeah, tell me about it simple_smile

You can switch to other frameworks with the same project btw, e.g. NHibernate or our own (although our own doesn't support value type definitions yet) Just right-click the framework name in project explorer. wink (just in case you get fed up with EF wink )

Frans Bouma | Lead developer LLBLGen Pro
amita
User
Posts: 5
Joined: 09-Mar-2015
# Posted on: 09-Mar-2015 17:44:08   

I have used LLBL Gen Pro to generate source code (data access layer) using Entity Framework version1 and version 4. Is there any samples or examples that explain how to use the generated Data access layer code ?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Mar-2015 06:31:47   

As you generated Entity Framework code, you should refer to Entity Framework documentation. In general, you should learn how to use the framework you are using (EF, LLBL, NHibernate, etc). LLBLGen Designer helps you to generate usable code for those framewoks but you have to check the documentation of them to know how to use them. In this case you should check Microsoft's EF documentation.

P.S.: Next time please open a new thread, as this one is very old and doesn't address the same subject of yours. Check our forum's guidelines: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7717

David Elizondo | LLBLGen Support Team