Property facet Nullable = None is overridden in Model.edmx xml on Update Model from Database - entity-framework

Using Entity Framework 6, I need to maintain a property facet value when using the Model wizard to update.
My property is not nullable in the database, but I have my property facet set as Nullable = None, and my model facet set as Update Property Facets = False. I have to manually edit the Model.edmx file to remove the Nullable="false" attribute from the property. The solution builds successfully in this state. The problem is when I do an Update Model from Database, the Model.edmx file adds back the Nullable="false" attribute and when I build I receive the error that "Error 3031: Problem in mapping fragments starting at line 999:Non-nullable column COLUMN_NAME in table TABLE_NAME is mapped to a nullable entity property."
How do I prevent the Model.edmx from adding the Nullable="false" attribute when running the Model wizard?
This is related to issue Designer: No way to mark facets of entity properties not to be overriden by database facets but slightly different.
I think this is an EF issue, but please note that we are using the NTier Entity Framework (NTIER)
#ChristofSenn Do you have any suggestions?

Apparently this is a known issue with no fix in the near future, as referenced on CodePlex. Property facet Nullable = None is overridden in Model.edmx on Update Model from Database

Related

How to create RowVersion column for optimistic concurrency using model first approach?

I am working on a legacy web app which implements model first(edmx file) approach using Entity Framework.
I need to implement optimistic concurrency, so I have added this field as following:
and inside the database has been created as binary(8) type.
But when I try to update the entity is getting updated but the VersionRow values is not updated(no new value generated).
P.S
When I added the column I have binded default value as 0x0000000000000000 because it does not allow null values.
Yea I solved it this way:
1) I changed the type of RowVersion column from Binary(10) into timestamp inside SqlServer.
2) In the property details inside the .edmx file I have put the property StoreGeneratedPattern of the property RowVersion as Computed.
Computed it means that a new value is generated on insert and update.
Now it became as following:

TableAttribute in EF 4.4, where is the old constructor with the TableName?

My project needs ef and we need to update to EF4.4 because of using .NET 4.0. So in this case, i changed the references to the new assembly. I get an build error that my TableAttribute(TableName) does not exists anymore. There is another Table attribute in DataAnnotations.Schema which does not take the table name. Im localizing automatically by using that Table attribute, so where is it gone in EF Version 4.4. Is there an alternative where i can add an aspect to the entity which specifies the table which is assigned later?

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.

How to preserve non-nullable setting in edmx when refreshing model?

I have a project that I recently migrated from VS2010 to VS2012.
It contains an EF4.1 edmx file containing my entities. A few of these entities are based on views and are read-only. Although some columns returned by the views are nullable, I have changed the return value of these columns in the view to ensure that they do not return null by using
ISNULL(NumericColumnName, 0) AS NumericValueColumn
When updating the model via the "Update Model From Database" function in VS2010, the columns that were returned in this way were being created as non-nullable in my model. However, since the move to VS2012, this is no longer the case.
Now, whenever I use the "Update Model From Database" functionality, these properties are generated as nullable. What I have to do is manually set the columns to non-nullable. However, the entity properties will be regenerated as nullable the next time that I run "Update Model From Database". I then have to update them to non-nullable again.
Another work-around that I have tried is to move the view-based entities to their own edmx file. However, I am seeing the same behavior when updating either of the edmx files.
Another detail is that I am using Self-Tracking Entities, generated via the EF4.1 T4 STE templates.
Is there any way to prevent the VS2012's EF designer from overwriting my non-nullable attribute for the view-based entities?
EDIT
It turns out that the designer in VS2012 is trying to prevent you from shooting yourself in the foot, and consequently changes the member properties inside my entity.
When generating a member from the following:
SELECT ISNULL(Quantity,0) * ISNULL(Number, 0) AS Total FROM SomeTable
VS2012 will always set the member Total on the SomeTable entity to nullable because it does not understand that there is no way for the result of the multiplication to be 0. To prevent this behavior, change it to:
SELECT ISNULL(Quantity * Number, 0) AS Total FROM SomeTable.
Watch out for aggregations as ISNULL(SUM(SomeCol),0) is not the same as SUM(ISNULL(SomeCol,0)).

How do I get Entity Framework to only update the Properties modified in the SQL generated?

I am using Entity Framework with the Self-Tracking Entity T4 templates, which by default will generate a SQL Query setting all the properties on the Entity in an UPDATE statement. I only want an UPDATE statement that contains the properties modified.
I modified the T4 Template as specified in the book: Entity Framework Recipes: A Problem-Solution Approach page 503.
I changed to this line in the T4 Template:
OriginalValueMembers originalValueMembers = new OriginalValueMembers(false, metadataWorkspace, ef);
Making the Entity track each property change instead of just tracking that the Entity changed.
And also
context.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
After making these changes I got the desired result of SQL statement with only the modified values/properties in the UPDATE statement. However, there was a strange side effect. When updating a nullable INT property from null to something other than null, the change of that was ignored by Entity Framework. The Self-Tracking Models show the change in the ChangeTracker with the accurate OriginalValue null, but when Entity Framework tried to generate the UPDATE SQL it did not see that property change if the original value null and the new value is not null. I worked if the original value was not null and value gets changed.
It seems to work ok on a string property going from null to a non-null value, but int? is not working.
Does anyone have any ideas?
If helpful, have found post that fixes this: fix update of nullable column.