Entity Framework EDMX Diagram table missing properties after update - entity-framework

I'm maintaining an asp.Net MVC 3 web application using Entity Framework 4.1 Database first approach. I have had to add two new properties to one of the tables in the database.
I then right clicked on the EDMX diagram and choose 'Update Model from Database' within the context menu, like so:
This works without any problems, but the two new fields aren't added to the expected table. However, when I open the EDMX file in XML format, I can see the two new fields listed, like so:
<EntityType Name="Shift">
<Key>
<PropertyRef Name="shiftID" />
</Key>
<Property Name="shiftID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
//Two new fields below
<Property Name="shiftTitleGradeID" Type="int" />
<Property Name="shiftTitleSubID" Type="int" />
</EntityType>
Can anyone advise me on how to get the two new fields into my EDMX diagram and not just the XML file?
Thanks in advance.

Got it fixed. I had to delete all references to the new properties in the XML file. Then recreate the update model from database procedure, this time ensuring the two checkboxes where ticked, Pluralize or singularize generated object names, and, Include foreign key columns in the model (I thought I had these ticked the first time, but I didn't).
This fixed my problem.

Related

Web API 2.2 OData V4 not recognizing primary key

In Web API 2.2 WebApiConfig.cs:
ODataModelBuilder builder=new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers")
Customer entity is generated by the entity data model wizard. "EF Designer from database"
When i try to load the entity it returns error "Customer entity has no key"
Even though the CSDL file specifies the primary key and it works with Web API actions
<Key>
<PropertyRef Name="CustomerID">
</Key>
...
The workaround has been to manually add the [Key] annotation attribute in the model.
QUESTION: Is this a bug? How can I resolve this issue, without having to manually edit anything to specify the primary key for each entity?
In Your CSDL example you are not closing the PropertyRef tag with "/>". This XML syntax error could make to ignore the Customer Key.
<Key>
<PropertyRef Name="CustomerID" />
</Key>
...

Entity Framework - entity using a view giving duplicate data

I have the following view (SQL Server 2012 if it matters):
SELECT
EntityId
,EntityType
,StateId
FROM
SomeTable
INNER JOIN SomeOtherTable
When I generate an entity for this view (EF 6 - database first) it looks like this in the EDMX file:
<EntityType Name="VW_MyView">
<Key>
<PropertyRef Name="EntityId" />
<PropertyRef Name="EntityType" />
</Key>
<Property Name="EntityId" Type="Int32" Nullable="false" />
<Property Name="EntityType" Type="String" Nullable="false" MaxLength="2" FixedLength="false" Unicode="false" />
<Property Name="StateId" Type="Int32" />
</EntityType>
As you can see, the model generator created an entity key on the first two columns. The problem is, the first two columns do not guarantee uniqueness.
So for example I could have data like this in the view:
EntityId EntityType StateId
-------- ---------- -------
1234 CR 1
1234 CR 2
1234 CR 3
When I query the data using linq such as:
using (ContextA context = new ContextA())
{
var zList = context.VW_MyView.Where(f => f.EntityId == 1234
&& f.EntityType == "CR").ToList();
}
I get a list of three items, but like this (notice stateid duplicated):
EntityId EntityType StateId
-------- ---------- -------
1234 CR 1 <-- dupe
1234 CR 1 <-- dupe
1234 CR 1 <-- dupe
I migrated this exact same code from EF 4 (object context templates) to EF 6 (dbcontext templates), and before the migration it did not perform like this.
I know I can manually add an EntityKey to the StateId column, and it will work properly, but I have over 100 views in my model and I don't want to go through each one to check.
Why has this behavior changed, and is there a setting I can enable (globally) to correct this?
EDIT:
So based on the answers, I have been able to gather three ways to prevent this issue.
Add all primary key values from each consisting table into the view
Use nullif() tricks in the view to force columns to be non-nullable, and those be added by EF to the key
Manually add the Entity Key in the model myself
But this doesn't explain really why this happens, and how it could possibly be desired behavior? The EF linq query is simply returning entirely incorrect data, without any exceptions or warnings. I can't imagine this is correct.
I have the same "issue" in EF4 (with an .edmx file using the ObjectContext database-first approach) - not sure why it worked for you.
For Entity Framework, if it doesn't have a specified primary key (like on a table), it will fall back to using all non-nullable columns of that object (here: your view) as its compound PK.
These non-nullable columns are now the key for the table/view, and thus, only one value of that key can exist.
In order to resolve this, you need to either include more columns in your view to make the auto-detected key really unique (by including e.g. the primary key of all underlying base tables), or you need to manually set the key properly to something that works for you.
Another solution I found is by setting entity's MergeOption to NoTracking.
using (ContextA context = new ContextA())
{
context.VW_MyView.MergeOption = System.Data.Objects.MergeOption.NoTracking;
//Rest code goes here...
}
Solution found in this thread

ORM to create single entity from more than one database tables

Well tested running system have already defined entity called 'User'.
Now I need to add a new property to User entity (ex: Age)
To do this in the safe way, I do not like to do any changes with the existing data base table, because that is very risky in my case. I need a way to rebuild the User entity with the minimum code changes.
So my proposal is:
Create a new table (user_age), with two columns (user_id, age)
Modify the user entity to add property 'age' and its getter-setters
So my entity (User) properties, will be saved to two different tables (user and user_age)
Loading the user is also similarly.
Is this possible to do with hibernate....??
If not, Any other safer way to do this with Hibernate...?
what are the available ORMs that provide this kind of feature (nhibernate, entityframwork,etc... or any other ORM)...?
Yes, there are various approaches:
[1] See JPA Secondary Tables. This allows you to map an Entity to two or more tables.
Section 2.2.7: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2235
[2] Create another Entity, say UserInfo, mapped to this new table. Create a one-to-one mapping from User to UserInfo.
Yes. You can do that.
I've used for a similar problem a joined-subclass.
Base:
<class name="User" table="Users">
<id name="Code" type="System.Guid">
<column name="Code" />
<generator class="guid.comb" />
</id>
...
</class>
Subclass:
<joined-subclass name="UserExt" extends=User" table="UsersExt">
<key column="Code" />
<property name="Age">
<column name="Age" not-null="true" />
</property>
</joined-subclass>
A good reference here.
NHibernate's join mapping is for exactly this case.
See Ayende's blog and the documentation for more information. From the documentation:
Using the <join> element, it is possible to map properties of one class to several tables, when there's a 1-to-1 relationship between the tables.
From my searches, it looks like it is also possible to do this with Entity Framework: Simon J Ince - Mapping two Tables to one Entity in the Entity Framework . I think this article is about Entity Framework v1, and things could have changed by now, but it appears that there is an important limitation in Entity Framework's version of this mapping:
... it requires a record in each table to exist as the generated SQL uses an INNER JOIN. It makes sense if you're using a new model, but I guess this is more tricky if you're mapping to an existing schema and data.
With NHibernate, you can set the optional attribute on the join mapping to tell it to use outer joins instead of inner joins.
optional (optional - defaults to false): If enabled, NHibernate will insert a row only if the properties defined by this join are non-null and will always use an outer join to retrieve the properties.

Distinguishing between storage model and conceptual model field names (Entity Framework)

Every sample I come across has the entities and properties in the storage model named exactly the same as in the conceptual model. So in the mapping section, I can't tell whether an entity or property is from the storage model or conceptual model.
This is a snippet of an Entity Framework diagram. Which "ID" fields are from the database and which are from the entities?
<AssociationSetMapping Name="FK_Orders_Customers" TypeName="ContosoModel.FK_Orders_Customers" StoreEntitySet="Order">
<EndProperty Name="Customer">
<ScalarProperty Name="ID" ColumnName="CustomerID" />
</EndProperty>
<EndProperty Name="Order">
<ScalarProperty Name="ID" ColumnName="ID" />
</EndProperty>
</AssociationSetMapping>
Well only databases have Columns, so ColumnName is the Database Name.
Name is from the Entity (or in this case the Association).
Hope this helps
Alex

What am I doing wrong to get "Two Entities with different keys are mapped to the same row

See images:
EF Designer
SQL Tables
"Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with overlapping keys to the same group of rows."
In fact, only one of the two (or 6 here) entities will have the key for the single row.
How may I overcome this? SQL View that combines them all to one row? Calculated discriminator column in ContactMethod? How should I have designed the tables differently to work better with EF?
Or is there some XML editing I can do to keep my schema and just tell EF to trust me that I'd never put the same Id in more than one derrived class?
Here's: edmx if it helps.
The link to edmx file is not work. Perhaps, you have to add a condition tag
<Condition ColumnName="ContactId" IsNull="false" />
to your edmx file using xml-editor:
<AssociationSetMapping Name="FK_Contact_ContactMethod" TypeName="SomeNamespace.FK_Contact_ContactMethod" StoreEntitySet="ContactMethod">
<EndProperty Name="ContactMethod">
<ScalarProperty Name="ContactMethodId" ColumnName="ContactMethodId" />
</EndProperty>
<EndProperty Name="Contact">
<ScalarProperty Name="ContactId" ColumnName="ContactId" />
</EndProperty>
<Condition ColumnName="ContactId" IsNull="false" />
</AssociationSetMapping>
Taking off the foreign keys to ContactMethod in the database got rid of the errors, but doesn't seem right.