Bug #53188 Entity Framework edmxgenerator does not set DefaultValue from the schema default
Submitted: 27 Apr 2010 8:02 Modified: 6 Apr 2011 20:39
Reporter: Michael Pritchard Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.3.1, 6.3.5, 6.3.6 OS:Windows (Vista SP2)
Assigned to: Julio Casal CPU Architecture:Any

[27 Apr 2010 8:02] Michael Pritchard
Description:
Create an ADO.NET Entity Data Model for a database table that has columns with default values in the schema. When the .edmx file is generated it does not set the DefaultValue attribute for any columns with a default value defined. 

This can then cause errors trying to create entities that only expose a subset of the tables columns as properties, when a non-nullable column with a default is excluded from that entity. The error text is:
"Error	1	Error 3023: Problem in mapping fragments starting at line 42:Column new table.string default in table new table must be mapped: It has no default value and is not nullable.
	C:\Users\...\Documents\Visual Studio 2010\Projects\TestEntities\ClassLibrary\Model1.edmx	43	15	ClassLibrary"

Will attach an example with broke and fixed edmx files. 

I am presuming this DefaultValue should be set, I am only just starting out on Entity Framework so apologies if its not.

MySQL: 5.1.31-community
VS2010 Professional (Final Release)
Connector: 6.3.1

How to repeat:
Create the following table:

CREATE TABLE `new table` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `not_null_default` tinyint(1) NOT NULL DEFAULT '0',
  `string default` varchar(45) NOT NULL DEFAULT 'default me',
  `string nullable default` varchar(45) DEFAULT 'nullable me',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Create a new blank Solution in VS2010 and add a single class library project. Add a new ADO.NET Entity Data Model to the class library and auto-generate from this table. If you view the generated edmx file you will see no DefaultValue attributes on the schema in the  <!-- SSDL content --> section. This does not generate an error.

Then in the model designer create a new entity, add two properties and map them to the id and not_null_default columns. Delete the auto generated entity for new table. You will then get the error listed above in the description shown in the Errors window

Suggested fix:
The DefaultValue needs to be set in edmx file.

The workaround is to manually edit the edmx file but for large databases this can be time consuming and I guess updating the model from the database will wipe any manual changes made
[27 Apr 2010 8:06] Michael Pritchard
test solution

Attachment: TestEntities.zip (application/x-zip-compressed, text), 36.38 KiB.

[5 May 2010 5:40] Tonci Grgin
Thanks Michael, let me see what I can come up with.
[30 Dec 2010 7:21] Bogdan Degtyariov
Verified with Connector/NET 6.3.5.
Also, bug 59176 is marked as duplicate of the current report.
[6 Mar 2011 19:19] Valeriy Kravchuk
Bug #60360 was marked as a duplicate of this one.
[6 Apr 2011 20:39] Julio Casal
The Entity Data Model (EDM) Wizard does not bring default values over to the
model’s SSDL. Such behavior does not belong to Connector/Net as the wizard is a Visual Studio component.

Given that, the only two possible known workarounds are:
1. Manually add the DefaultValue attribute to the properties that require it by editing the SSDL xml.
2. Completelly remove these properties by editing the SSDL xml.