views:

327

answers:

1

Hello,

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.

  1. 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'

  2. 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.

+1  A: 

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.

LukLed
thanks. its more a workaround, but at least it does the job. i wanted more the let the db do the work (eg. sql timestamp / datetime, etc)But by using an OR Mapper i guess this is not possible this time.
csharpnoob
@csharpnoob: It is definitely possible to do on db side. Look at StoreGeneratedPattern: http://msdn.microsoft.com/en-us/library/bb738536.aspx
LukLed