Entity framework - having the same table in more than one edmx - entity-framework

I have two edmx files .
Both contain the table "applicationType".
That means I have two objects applicationType with different namespace.
When I try to query from one
I get the following error -
"Schema specified is not valid. Errors: \r\nMultiple types with the name
'ApplicationType' exist in the EdmItemCollection in different namespaces.
Convention based mapping requires unique names without regard to namespace
in the EdmItemCollection.\r\nThe relationship
'myModel.FK_ApplicationAttribute_ApplicationType' was not loaded because the type
'myModel.ApplicationType' is not available.\r\n"}
System.Data.EntityException {System.Data.MetadataException}
can anyone suggest how to query it ?

If the two Models are in the same namespace, you must have different names for these objects. Eg:
Model 1 - namespace: myApp - table: applicationType - entityName: applicationType
In your second model you must use another namespace, or, another entityName.

Open one of yours edmx diagrams
right click at the replicated entity and rename it
Save it, build the project
Adjust errors that possible will appear with code that uses the entity you just rename (only update entity name).

Related

Using the same entity type for multiple entity sets (Entity FrameWork)

I have a table and a view which share the same columns and I'm trying to add them to EDM. I created an abstract entity type and 2 derived types A and B.
Initially I tried creating 2 entity sets, one for each of the derived types, and proceeded to add the EntitySetMapping (using IsTypeOf per MS MSL specifications for EntityTypeMapping of derived type) but got an error in the .edmx file that each of the types doesn't have an entity set and isn't mapped.
To solve this, I created a single EntitySet with type as the base EntityType (the abstract one) and used 2 EntityTypeMapping children tags, specifying the StoreEntitySet as the table or the view accordingly. This resolved the issue but created a single DbSet<BaseType> and now I cannot retrieve data from the view or the table using DbContext.
Any ideas on how to properly solve this?
Thanks

How to use Entity, Mapper, Service and Hydrator in ZF2

I am making a ZF2 app. I am using entities, mappers and services (e.g. UserEntity, UserMapper, UserService) to manage the objects/models. Properties in the entities are CamalCased (e.g. FirstName, LastName) while in the database, fields are using underscore (first_name, last_name). I will plan to use a hydrator to map the properties and db-fields when retrieving or saving. The service object (UserService) will be used to communicate with the mapper to retrieve and save data models using the mapper. The hydrator will convert the result of mapper and convert them into proper entities.
The thing I am confused is that when the service (UserService) need to provide some cirteria - for example to find all users with a specific 'last name', will the service use the database field names (last_name) or entity properties name (LastName)?
If the db field name is used in the Service, so any change in the db structure will require me to update the service also, which completely fails the reason of using the whole approach.
If you take a look at the ClassMethods:hydrate method (https://github.com/zendframework/zf2/blob/master/library/Zend/Stdlib/Hydrator/ClassMethods.php) you will see that it just copies the properties from one object to another. You have the option of converting the property names to/from camelCase but that's it.
If you change a column name in your database then you will need to change corresponding property name in your object. And vice versa. Which I believe is the crux of your question?
If you want to make table column names be independent of your method names then you need something that lets you define an actual mapping table somewhere. Change a column or method name and you only need to update the configuration mapping table.
Not a ZF2 expert so I could be wrong but it doesn't look like any of the supplied hydrators support this.
I do know that Doctrine 2 supports it.

Entity Framework 4.1 Complex Type reuse in different models

Here is the senario for which I could not find anything useful. Maybe Im the first person thinking of doing it this way:
Approach: Database First
Database: SQL Server 2008 R2
Project : DLL (Data Access)
I have a data access library which encapsulates all the access to database as well as the biz functionality. The database has many tables and all the tables have the following 2 columns:
last_updated_on: smalldatetime
last_updated_by: nvarchar(50)
The project contains several models (or edmx files) which contain only related entities which are mapped to the tables they represent. Since each of the table contain the columns for last_updated_* I created a complex type in one of the models that is as follows:
Complex Type: History
By (string: last_updated_by)
On (DateTime: last_updated_on)
The problem is that it can only be used in the model in which I defined it.
A) If I try to use it in other model it does not show it in the designer
B) If i define it in the other models I get error History already defined
Is there any solution so that the History complex type, defined in one model can be reused by other models?
I was trying to do almost the exact same thing (my DB fields are "created", "creatorId", "modified", and "modifierId", wrapped up into a complex type RecordHistory) and ran into this question before finding an answer...
http://msdn.microsoft.com/en-us/data/jj680147#Mapping outlines the solution, but since it's fairly simple I'll cover the basics here too:
First create the complex type as you did (select the fields in the .edmx Designer, right click, select "Refactor into New Complex Type")
In the .edmx Designer (NOT the Model Browser) right click on another table/entity that has the same common columns and select Add -> Complex Property
The new property will be assigned a complex type automatically. If you have more than 1 complex type, edit the new property's properties and set the Type appropriately
Right-click on the table/entity again (in either the Model Browser or Designer) and select Table Mapping
Update the Value/Property column for each of your common fields (changing them, in my case, from "modified : DateTime" to "history.modified : DateTime")
Delete the old common fields from your entity, leaving just the complex type in their place

EntityFramework inheritance - Ignore not nullable column

I have an one entity in my edmx model having an one property that can contains huge XML data.
Basically I want to load this entity without this property (column) /* huge data loading */ . And load this column only when it is strictly needed.
I have tried to create an inherited entity containing this property and remove this property from base entity (original entity). I have done mapping.
At this time I have problem, that during compilation a I get error, that base entity is not capable to insert and update itself, because property is not nullable
I am looking for best approach (solution) how this situation should be solved.
I am attaching the cut-out from my emdx designer (containing my current and desired situation)
UPDATE:
I will try to write a procedure that I have tried:
I mapped functions to my custom functions. For entity TRP_TechReport_T without the XML column (property). Then I just mapped for entity TRP_TechReport_T functions to my custom function (containing XML column).
Then I set Mapping condition on the entity TRP_TechReport_T: When TRP_XML = Empty.String
TechReport_T mappings:
TechReport_T functions:
TechReportFull_T mappings:
TechReportFull_T functions:
At this moment I get error:
Error 2 Error 3032: Problem in mapping fragments starting at line 3754:Condition member 'TRP_TechReport_T.TRP_XML' with a condition other than 'IsNull=False' is mapped. Either remove the condition on TRP_TechReport_T.TRP_XML or remove it from the mapping.
The column is not nullable in the database and mustn't be.
I can hard-set XML property to nullable, but in the case of the model updating from the database information will be lost.
At the moment it's the only thing I could think of.

Plural table names with Entity Frameworks Model First

I'm giving EF Model first a go. I'm using EF 4.1
Pretty much followed this article
I've set PluraliseNewObjects to False on the Model and also in Options->Database Tools ->O/R Designer set Pluralization of names to false.
Neither have any effect - when I generate a new schema from the model the table names are always pluralised - is it possible to disable this?
OK - I've found one way to achieve what I want - but it's a pretty daft route.
Generated db with the plural names (interesting that it only pluralised the tables mapping to types - not the auto-generated linking tables for many to many joins).
Manually renamed the tables in the database
Deleted Model from the project and recreated based on existing database schema (the one I've just renamed).
Model is now correctly mapped to singularly names tables.
I'll wait and see if anyone comes up with a more sensible way of achieving this....
The names of the tables in the generated DDL seem to match the "Entity Set Name" values (different than the "Entity Name"). If you singularize the Entity Set Names, the table names in the DDL are singularized as well.
This will have the possibly undesired effect of singularizing the EntitySet property names in your code, though. Instead of:
myDatabase
.Products
.Where...
.Select...
your code will look like:
myDatabase
.Product
.Where...
.Select...
may or may not be an issue