edmx not taking defaults from DB - entity-framework

Here is my scenario. I have a SQL Server database that I am using as the backend. And my application is in ASP.net. I have defined defaults for 2 of the columns in the DB table, table name is Car.
One of the columns in the Car table is called Qty and it's type is Int. I have set a default for this column in the database as 0 (zero)
Another column in the Car table is called Model and it's type is nvarchar(50). I have set a default for this column in the database as *
I want these defaults to show up in the edmx as well. My intention is that when I declare a new instance of class Car in code, I want the default value of Qty to show up as 0 (instead of null) and I want the default value of Model to show up as * (instead of null).
So after doing quite a bit of reading I realized that I need to set StoageGeneratedPattern = Computer for both Qty and Model in the edmx file.
It works perfectly for Qty. When I define a new instance of class Car and look at the properties, I see that Qty is 0. BUT for Model, the default is still null (instead of *).
Here is what I have done. I have set nullable = false and StorageGeneratedPattern = Computed, in the CSDL content and in the SSDL content of the edmx file. I still can't seem to get * as the default value for Model. What am I missing here?

Related

value from database computed property is Null in codefluent entity property

My MS SQL 2014 database table has a computed property column which uses a database function. Using SQL Server Management Studio, a query against the table lists the computed property values as expected.
The Codefluent model created via the import wizard shows the Entity with the computed column as a property. The underlying .cpf file defines the property with "d3p1:compute=" and the list of parameters that are used by the database function.
When an entity or the collection of entities is loaded, the properties which are used in the computed property have values, yet the computed property has a value of nothing/null.
How do I get Codefluent to read the computed value from the database table and have the value included in the entity's properties?
This is a bit tricky. First of all, you should declare the property like any other property. Then you must instruct the SQL producer to declare a formula on that column. You can do that with a custom 'compute' attribute in the SQL producer namespace. You can set it with the Visual Studio modeler like this:
In this example I've created an int property that is just another column value multiplied by 2.
Optionally, you can declare the property to be 'read on save' because most of the time, you want to read the computed value after a save, not only on load operations:
Once this is all done, this sample console app should display 30:
class Program
{
static void Main(string[] args)
{
var c = new Customer();
c.Name = "killroy";
c.Age = 15;
c.Save();
Console.WriteLine(c.Age2); // will display 30
}
}
If Simon Mouriers solution solves your problem than that is probably the best approach. However, there are 2 other options
RAW View Method
After you create a Codefluent Entities View click on the Edit Where button and it will allow you to create a RAW View
You can than specify the advanced property "UsedForMethods".
WARNING: Related entities will use the table instead of the view. This is by design and there is an article somewhere on the knowledge center on how to get around it. http://www.softfluent.com/product/codefluent-entities/knowledge-center/
Rename SQL Tables and Create a SQL View with the Same Name as the Original Table - This method is a hack, Softfluent discourages this approach, I love it because I know exactly what is happening under the scenes. I have used it with success in a scenario in which I needed soft deletes. I have automated the process with 2 stored procedures that handle the renaming. Using this approach requires running one of the stored procedures to undo the name changing prior to building the model. The other stored procedure handles the renaming after building the model. I'll post the stored procedures and how I use them within a couple of days.

JPA - Default value

I am developing an JPA application, and I'm using eclipse link provider.
I've a table T1 that has a field F1 defined as not null and with a default value ('U').
I've defined an entity mapped to the table T1. The entity has an attribute mapped to the field F1, and I have configured the attribute with the annotation #ReturnInsert(returnOnly = true).
Documentation says:
Use #ReturnInsert to cause INSERT operations to return values back into the object being written
If I persist an entity with no value in the attribute mapped to F1, everything works fine. The entity is automatically populate with the default value after the insert, and a record is inserted in database.
But if I try to persist an entity with a different value (no default value), a record is inserted into database, but the value of field F1 is the default value, when it should be the value I set in the entity.
Why is happening this? All I want is to change the default value.
Thanks
What I did to have default value for a field in my database was:
Define a default value for that field in database
Make the corresponding attribute in your entity to read-only.
for example for a Date field you can do like this:
in DB:
reg_date DATE NOT NULL DEFAULT CURRENT_DATE
in Entity:
#Basic(optional=false)
#Temporal(TemporalType.DATE)
#Column(name = "reg_date", insertable= false)
private Date regDate;
By doing this I got a field that always be automatically set by database when a new field inserted.

EF - eliminating nullable fields without changing database

We have a large legacy transactional-type SqlServer database with hundreds of tables, and most of the fields in these tables have the Allow Null attribute turned on.
We are also using Entity Frameworks and are developing a new C# to access the database. In the POCO classes generated by EF, the field properties have, of course, the Nullable decorator, which produces nullable data types (e.g., "int ?").
The impact is that if any given field contains null and we don't explicitly check for null before accessing the field, the app will blow up. Each field also has to be casted for use, or the Value property has to be used.
We could, of course, changes the schema and turn off the Allow Null property, but are getting push-back from the DBA's who want to avoid making any changes.
Is there a way in the EF generator to turn off the Nullable attribute for some or all fields, and coerce the logic to return default values for null fields (e.g., 0 for int, false for bool, etc.) on select statements, and to use default values for inserts/updates?

EF Table per Hierarchy (TPH) not saving because can't insert value null in Discriminator column

I have a table used for multiply type of category and it contains a Discriminator column named 'ClassName' to specify the type of object to load. The ClassName column is non nullable with a default value of 'Category'
My problem is when saving a new item , I get the error :
'Cannot insert the value null into column ClassName' table Category.
I tought that ef would set the ClassName value base on the new object class.
How can I save my object with the right 'ClassName' value ?
I changed my db Structure to accept null. EF will set null if the object name match the table name and will set the discreminator name for the derived classes.
It's old question but I just met it today. In .net 4.0 code first I don't meet "disclaimer column" but when downgrade to net 3.5, it make me tired for a day.
This is my solution
Change the column in the database to ALLOW NULL (using alter table)
and update the edmx file to make it update the change(allow null)

Supporting default column values in custom Entity Framework provider

I'm working on a custom entity framework provider and I need to add support for default column values for this provider. When the user uses the entity framework wizard and selects a table that includes columns with default values, those default values are not being populated into the entity designer.
I'm a little lost on where exactly this population should take place. I believe the appropriate place would be in the GetEdmType method override of DbXmlEnabledProviderManifest but I just don't see how to set the default value, if this is the correct place.
Anybody has experience writing EF providers that support default values for table columns? How do you implement this?
I am a bit late to the party but DbXmlEnabledProviderManifest is not the right place for adding default values. The provider manifest describes capabilities of the database engine itself and is specific (and general) to this database engine and not to a given database and/or table. The default value in the provider manifest tells EF what value to use for the given column property if one is not provided by the user (e.g. if the user user does not specify scale or precision for a decimal column the value from provider manifest will be used for scale and/or precision used for this column).
If you want just to insert a default value for a property the easiest way is to set the property that corresponds to the column on your entity to this value in the constructor. This way the user can always set it to a different value but if s/he does not the default value will be sent to the database. For some corner case scenarios where some of the columns in the database do not have corresponding properties on entities you can use DefaultValue attribute on the Property element in SSDL which will be inserted to the database when you add a row. This is especially useful if those properties are not nullable since without telling EF what value should be inserted EF would try inserting null which would obviously fail for non-nullable columns.