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

I looked at Entity Framework 4. When writing code for SQL Server 2008, I came to a point where I want to define some default values ​​for some fields.

  • how to define in the constructor for the created DateTime field the default DateTime.Now value?

    -> Error 54: The default value (DateTime.Now) is invalid for DateTime. The value should be in the form "yyyy-MM-dd HH: mm: ss.fffZ '

  • how to make the field string unique for code generation. Like E-Mail or Username, it can exist only once in a table.

I have searched a lot on the internet and also checked my Pro Entity Framework 4.0 and Programming Entity Framework books. But none of them seem to cause an issue with defaults or use sql commands as defaults to generate the database.

Another thing is how to prevent the database from always being generated from pivot tables? Instead, I want to add non existing fields and save the data.

Thanks for any suggestions.

+2


a source to share


1 answer


You can always define a partial class and initialize the value in the costructor:

public partial class MyEntityClass
{
    public MyEntityClass()
    {
        CreationDate = DateTime.Now;
    }
}

      



If the field is unique, you will have to validate it yourself and include it in your validation logic.

+2


a source







All Articles