Does Entitiy Framework set Default value - c#

Hi my problem is that my EF 4.0 mapping does not replicate the Default value of some columns.
For example
in my Question object i have an int type field Status for which i have set Allow null = false (un-check the allow null checkbox) and set its default value to be 1.
Now the strange behavior is when i insert the record in sql server and doesn't set the status field, it works fine and set the value 1 in record after commit.
But my entity framework mapping (.edmx file) doesn't set the default value and it says "Default value = (None)" in property window.
Is that a known issue or i am missing something?

I had this exact same issue a few months back and I did not find any easy resolution to the problem.
I can only suggest that you manually decorate your entities from within your edmx or if possible upgrade to EF 6 and use the Code-First from database option which from my experience works much better.

Related

How to set a property value to auto increment in visual studio lightswitch 2012

i want to set a property value in a table to default auto increment,but their are no options to do so in lightswitch2012 to my knowledge which is given that i recently started learning lightswitch,very light.
ok heres the real problem,this is the table
[customer][id,customer_id,name]
i want to set customer_id by default to id unless it is manually changed to different value.
how to acheive this?
In the Entity designer make your Customer_ID not required.
Write Code for Customers_Inserted.
Then, check to see if the Customer_ID is null. If it is, copy the ID field to it.
private void Customers_Inserted(Customer entity)
{
if (entity.Customer_ID == null) {
entity.Customer_ID = entity.ID;
}
}
You're right, there is no "auto-increment" data type available in LightSwitch. The ID property auto-increments, but that's a special case, handled by LightSwitch.
If you were attaching to an external SQL database, if you added a column that was an Integer Identity column, although it'll just appear as an Integer property in LightSwitch, it would still auto-increment because that's actually done in the SQL database itself.
The problem with all auto-increment properties is that you won't get the actual value until the record is saved.
Can I ask why you need an auto-increment property?
I may be misunderstanding what you are trying to achieve, but if you are using either a table or a grid, and you want to set the values for various entities for each new row your user adds (like customer_id = id, etc.), you can use the _Changed method and Add event to programmatically set any of the new row entities.
If this is along the lines of what you're looking for, watch Beth Massi's video How Do I: Copy Data from One Row into a New Row? You should be able to adapt her code to accomplish what you have in mind I think.

Entity Framework translating number(1) to Boolean help?

Im using Devart dotConnect for Oracle - Entity Framework + .NET 3.5. When i create the Entities, the framework change those colums type number(1) into bool. I read that it is a feature of every kind of entity framework. But i don't want it.. We use here number(1) to specify status of an db object.. like Clients - Situation.
I tried to set manually the .ssdl "bool" to "int" and .csdl "Boolean" to "Int32".. it just keep the same error of convert... if i use the "Run Custom Tool" it changes back the field to bool and Boolean.
I tried to change de property manually too in the Diagram.. w/o sucess.
In your Model explorer you will need to change the datatype of the column in the table in the ".Store" part of the model as well. The datatype of the class.property in your xxxModel part needs to match with the datatype of the table.column in your xxxModel.Store. If you choose Int32 in your class, you could choose Decimal in the table.

How to assign default values and define unique keys in Entity Framework 4 Designer

I've had a look at the Entity Framework 4. While generating code for the SQL Server 2008 I came to the point where I want to define some default values for some fields.
how to define in the designer for a Created DateTime Field the DateTime.Now default value?
-> Error 54: Default value (DateTime.Now) is not valid for DateTime. The value must be in the form 'yyyy-MM-dd HH:mm:ss.fffZ'
how to make for code generation a string Field unique. Like E-Mail or Username can exists only once in the table.
I've search alot in the internet and also checked my books Pro Entity Framework 4.0 and Programming Entity Framework. But none of them seems to come up with the default value issue, or using sql commands as default values for database generation.
Another thing is, how to prevent on database generation always from droping tables?
Instead i want to append non existing fields and keep the data.
Thanks for any suggestions.
You can always define partial class and initialize value in costructor:
public partial class MyEntityClass
{
public MyEntityClass()
{
CreationDate = DateTime.Now;
}
}
If field is unique, you'll have to check it yourself and include in your validation logic.

How can I use validation to enforce the uniqueness of a property in ASP.NET MVC 2?

Imagine an object with a field that can't have a duplicate value in the database. My first instinct was to create a unique attribute that I could apply as a data annotation to a property. This unique attribute would hit the database and check if the value already exists. This would work when executing a create method, but would fail on an update. On an update, I would get a duplicate value error for every unique field of my entity whose value I don't want to change. What would be a good way, or an established practice, to accomplish this on ASP.NET MVC 2 in a way that fits nicely with the ModelState? Passing the id of my object to the attribute validator could work by checking if the duplicate value that is found is of the same entity that I am updating but I don't know how to get that data from inside of the validator.
Please forgive me if this is a stupid question or if it is phrased incoherently. It's almost 3 in the morning and I've been coding since the morning of yesterday.
For this kind of validation, I would let the database do what it already does so well. Make sure your database has the unique constraint and let it report back an error if you violate it. You can then add the error to the model errors (with a nice friendly bit of text, rather than just plonking the SQL error).
If you are determined to perform a check yourself, you can get around the UPDATE problem by excluding the current record...
SELECT COUNT(*)
FROM myTable
WHERE myTable.UniqueValue = 'ShouldBeUnique'
AND myTable.Id <> 5
In this example, you use the id of the record you are updating to avoid checking it, which means you just check other records to see if they contain the unique value.

Set the Default Value for a property in an ADO .NET Data Service

I have an Entity Data Model being read by an ADO .NET Data Service.
The entity in question looks like this:
**PaidAmount**
Id
FY_1993
FY_1994
...
FY_2030
Is it possible to set a default value so that values of 0 aren't serialized?
I have tried setting FY_1993's Default Value to both 0.00 and 0 but when I view the Entity in the .svc the properties still come though as <d:FY_1993 m:type="Edm.Decimal">0.00</d:FY_1993>
Any Ideas?

Categories