Databinding and Nullable does not seem to work together - entity-framework

Can someone explain why data binding against a nullable property in an ADO Data Entity Model does not seem to work. I have a decimal field named "Weight" that when it is set to allow nulls in the database, I can not seem to change the value on a windows form. However if I turn off the allow nulls in the database and update the model, the databinding works as i expect. Looking at the Designer.cs file the only difference I can tell is the underlying value seems to be a decimal versus a decimal?.

It may be related to this nasty bug (there was a microsoft connect bug report, but the EF team apparently deleted all the bug reports...)

Related

Sudden massive EF materialization errors

We are using Entity Framework 6.4.4 in our project and have ran into a serious problem recently. After running for 10-20 hours (maybe around a million requests) we suddenly get a massive amount of EF materialization errors. Looks like all EF mappings get messed up for some reason and wrong properties are being used even for basic methods like Find.
Couple examples of the errors we receive:
The '...' property on '...' could not be set to a 'System.Byte' value. You must set this property to a non-null value of type 'System.Guid'. at
System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) at lambda_method(Closure , Shaper )
Shaper....SingleOrDefault[TSource]_InternalSet`1.FindInStore_InternalSet`1.Find
The '...' property on '...' could not be set to a 'System.Byte' value. You must set this property to a non-null value of type 'System.DateTime'
All of this code works until some specific moment so it does not look like an issue with our existing mappings. We were not able to find anyone similar issues so far. Perhaps upgrading to EF Core would resolve them but that would require a huge time investment so right now we are looking for a temporary solution.
Does anyone have information as to what could be causing this? We hope to at least figure out if this is mostly caused by heavy load so we can optimize and scale our code or some sort of error on our part.

Cannot See Why SQL is Searching a Discriminator Column

After having read a swag of SO questions about the Invalid Column Discriminator, I cannot see why I am running into the issue.
I have reverse-engineered a database Code-First using the EF Power Tools Visual Studio extension.
I have got an include path of "OrderRow.OrderRowOptions.Option" coming off my Orders DbSet. That is,
context.Orders.Include("OrderRow.OrderRowOptions.Option")
There is no Discriminator column on any of the tables and no Discriminator property in any of my POCOs.
The SQL which is being generated and sent to the database includes a Discriminator column.
Why? It's not like they are out of sync. I've repeated the reverse-engineering in a Console app just to make sure of that.
I believe I have finally figured out the problem here.
I always knew from the outset that it was related to EF's treatment of lookup tables.
And I knew that the Table Per Heirarchy ("TPH") feature was in play here.
The bit that I did not realize what that there does not actually need to be a Discriminator column.
I do have a TPH as there is a type called AdditionalLocationsOrderRowOption which inherits from OrderRowOption.
So, even though there is NO Discriminator column in either my database of my domain types (I've verified this countless times), the TPH is coming into play and preventing me from doing the Include on the Orders items.
So, my next step will be to use the Join method of EF-to-entities.

Passing a session variable to the table update stored procedure in Entity Framework

How do I pass a session variable to the Insert, Update, Delete stored procedures mapped to a Table in an Entity Framework, and how do I get the mapping in the EDMX diagram to work with it?
I have a database with Stored Procedures (SPs) defined for Inserting, Updating, and Deleting rows. The SPs expect a userid parameter to be passed to them for audit trail purposes, this parameter does not exist as a column in the table.
When I'm in the EDMX diagram specifying the mapping for the Parameter I don't see any way to either a) ignore the parameter or b) (preferred) set the value to a Session value. I've created a partial class mapping of the Stored Procedures, however the model doesn't seem to see it or even acknowledge that it is there. The whole premise behind using this technology is to let it do the work. I can't even tell it to ignore this parameter as it is optional and has a value assigned to it. I can't even edit the SP definition as the file is marked autogenerated, and will lose any modifications when it is regenerated.
Entity Framework seems like a well thought out approach, and I've looked at several tutorials and books on the subject. However, this seems like a really obvious thing to do. I've looked through stack overflow and the MSDN forums and found similar questions from years ago, but no one ever seems to answer the question, they just dance around the issue.

entity framework and database default values workaround

I have to decide about an important item and I need your help.
I'm facing an huge existing database with a lot of default values on nullable columns.
The team has to build a new MVC4 application on top of it (in fact it is a rewrite of old VB6 application).
I (as a consultant) have 'forced' the use of EF5 to get rid of all stored procedures and migrate to a more modern techology.
Now, after my research, it is clear to me that EF5 doesn't support database default values per default. This is why my inserted records are corrupt (they are inserted because the columns are nullable, but with NULL of course).
Some options came up like using the constructor technique, setting the default values in design on the edmx, or playing around with the xml of the edmx.
Despite, these methods are not usefull for us. Where the constructor technique looks ok for me, it is not feasible to do that for all tables in the DB. I also have a 'njet' from the technical person because he wants to maintain these values on 1 place. Same story for setting the default values in design. The database is also not in our scope (read: as less as possible changes to keep existing applications running).
At this point, I'm not sure it EF is the correct choice for our project.
Is somebody aware of (3th party) tools that can fill in the database default values automatically in the generated xml of the edmx file?
Is there som more info about how this xml is build and if there is a possiblity to interfere in the process?
Is there a good readon why these default values should not be taken? Is this going to change in a later release?
Are there other good practices that can be applied to that problem without having all values dupplicated or a massive workload?
Can I arrange something with my poco generator?
I realize there are already a lot of posts of this topic. Too bad, there is no suitable solution for me since we have already something existing and (with all respect) an old VB6 team that I have to convince.
Thanks for your feedback!

Entity Framework: Ignore Columns

I have a database that I wish to build an EF model from, however I do not want to include certain columns from the database as the columns concerned are maintained exclusively on the server and should not be manipulated by any application.
Both of the columns are DateTime (if this makes any difference), one of the columns is nullable and is maintained by a trigger on updates and the other is not nullable and set using a default value in the table definition.
I guess I am looking for something like the "Server Generated" option in Linq2Sql; but I cannot find such an option.
Can anybody tell me how to work around this?
Caveat:
I have been trying to introduce business object modelling at my place of work for some years and it has always been rejected because of the amount of additional code that has to be hand-cranked. EF is currently being seen as a viable solution because of the designer and code generation therefore any option that involves hand-cranking the XML will only turn the rest of my colleagues away from EF. I am therefore looking for something that can be done either using the designer or using code.
EDIT:
I guess that what I am looking for here is either...
(a) a way to create the model without EF referencing the columns in the store (ssdl) and therefore not looking to manipulate it in any way
(b) a way to programatically set the "StoreGeneratedPattern" attribute against the property when I create the ObjectContext (the easy answer is to manually manipulate this in the .ssdl, but this would then be overwritten if I refreshed the model from the database and I cannot go down the route where the .csdl, .msl & .ssdl are hand-cranked).
Can you do this with the Entity Framework? Yes; it's easy. Can you do this with the Entity Framework designer? Unfortunately, that is much harder.
The problem you're having is that the column exists in the storage schema (SSDL) in your EDMX. Removing the column with the GUI designer simply removes it from the client schema, not the mapping or the storage schema. However, it's simple enough to go into the EDMX and remove it. Having done that, you can also remove it from the mapping in the client schema portions of the EDMX, and the entity framework will longer complain that it is unmapped.
Problem solved, right?
Well, no. When you use the GUI designer to update the EDMX from the database, the storage schema is thrown away and re-generated. So your column will come back. As far as I know, there is no way to tell the GUI designer to never map a particular column. So you will have to re-do this every time you update with the GUI designer. Fortunately, the EDMX is XML, so you can do this with a XML transform, LINQ, or the XML tool of your choice.
Can you not create a view with the columns you need and import it through entity function wizard and map it to your entities?
You could modify the text template to ignore these columns when generating your entity classes. For example if you added "IGNORE" to the documentation summary, you could modify the template to ignore them by replacing;
Dim simpleProperties as IEnumerable(Of EdmProperty) = typeMapper.GetSimpleProperties(entity)
with;
Dim simpleProperties as IEnumerable(Of EdmProperty) = typeMapper.GetSimpleProperties(entity).Where(Function(p) p.Documentation is nothing orelse p.Documentation.Summary.IndexOf("IGNORE")<0)
Right click on the field in the graphical representation and choose delete. Ive found that sometimes you will get errors when you make a lot of changes to the modeling at once and start to lose track of your changes. Your best bet might be to rebuild the EF generated model.
Keep in mind that when you "update from the database", that old fields on the generated models will not be removed, you will have to remove them manually. For example if you renamed DateField1 to DateField2 in your database, and then you "Update Model from Database", you will now see both DateField1 and DateField2 on the resultant model. This can be a cause of errors.
Do you not want the column to appear in the model at all?
Try selecting the column in the Designer view and hitting the delete key.
Edit
You could make the setter for the property private. Then your app won't be able to modify the value.
Timestamp is a different data type than DateTime. Timestamp seems to be recognized as an attribute the engine manages, much like an identity attribute. You can't "update" a timestamp attribute. Hence, the EDM can manage it correctly (just as it does an identity).
In EDMX designer, select the property and set StoreGeneratedPattern to Computed.