none
How can I set the unique constrain for one property in entity framwork by Fluent API? RRS feed

  • Question

  • I have an entity has two properties

    Emplyee

    {

        Guid Id;

        String EmployeeNo;

    }

    How can I use Fluent API to set the property in Database with unique constrain?

    like: this.Property(e => e.EmployeeNo)
                    .IsUnique();


    : )Ex

    • Moved by Mike FengModerator Monday, November 26, 2012 5:37 AM (From:.NET Framework 一般性问题讨论区)
    Friday, November 23, 2012 7:31 AM

Answers

  • Hi Ex.Shen;

    To your question, "How can I use Fluent API to set the property in Database with unique constrain?", currently Entity Framework as of version 5.0 does NOT support unique constrain. From the update at this Microsoft web site, Unique Constraints in the Entity Framework , which states, "Update: this feature has been postponed and will not be included in Entity Framework 5".

    Mr. Miller of Microsoft has posted a work around for this issue and can be found on his web site, "Ef Ctp4 Tips & Tricks: Running Additional Ddl",  Although it references CTP4 it should work on all versions.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Ex.Shen Thursday, November 29, 2012 4:42 AM
    Monday, November 26, 2012 3:25 PM

All replies

  • Hi Ex,

    I moved this thread to English forum since you post it in English.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 26, 2012 5:38 AM
    Moderator
  • Hi Ex.Shen;

    To your question, "How can I use Fluent API to set the property in Database with unique constrain?", currently Entity Framework as of version 5.0 does NOT support unique constrain. From the update at this Microsoft web site, Unique Constraints in the Entity Framework , which states, "Update: this feature has been postponed and will not be included in Entity Framework 5".

    Mr. Miller of Microsoft has posted a work around for this issue and can be found on his web site, "Ef Ctp4 Tips & Tricks: Running Additional Ddl",  Although it references CTP4 it should work on all versions.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Ex.Shen Thursday, November 29, 2012 4:42 AM
    Monday, November 26, 2012 3:25 PM