none
What if I want to create a "second index" for an entity (in addition to the entity key)? RRS feed

  • General discussion

  • WHAT I HAVE:

    Visual Basic 2010, .NET 4.0, Entity Framework 4.0, SQL Express

    MY PROBLEM:

    Suppose I have an entity called Person, and it includes as properties Id, FirstName, LastName, and perhaps others. I want Id, an identity property, to be the entity key, but I also want a unique index based on FirstName and LastName so one can't inadvertently create two people with different Id's but the same names. What is the best way to do this? (Ideally, the solution should use high-level EF so it can be used on other projects with minimal code modification. For instance, are there methods for getting database table/field names from the conceptual model's entity/property names, and vice versa?)


    Robert Gustafson




    • Edited by RobertGustafson Sunday, May 25, 2014 6:53 AM
    • Changed type Fred BaoModerator Monday, June 2, 2014 5:41 AM The discussion type is more suitable for this case
    Saturday, May 24, 2014 9:31 AM

All replies

  • Hello,

    >>What is the best way to do this?

    In my opinion, it is best to create the composite unique index in database.

    >>For instance, are there methods for getting database table/field names from the conceptual model's entity/property names, and vice versa?)

    The MeteDataWorkSpace may be helpful.

    There is an example:

    http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 26, 2014 7:40 AM
    Moderator
  • Thank you for providing half of what I'm looking for.

    I visited the link you provided, and converted the code for the "alternative approach" (which uses the metadata workspace instead of a dummy query and regular expressions) from C# to VB. Now I can convert an entity's name to its table name, but I still need to be able to convert property names (including complex properties) to table field names. Since I'm not fluent in using the metadata workspace, can you give or direct me to the correct code for the latter conversion?

    (It's my intention to create database instances programmatically at run-time--specifying catalog and file names in the connection string and using the ObjectContext.CreateDatabase method--and then create the additional indexes using CONSTRAINT SQL and the ObjectContext.Execute method. For that, I need to be able to programmatically convert both entities into tables and properties into fields.)


    Robert Gustafson






    Wednesday, May 28, 2014 11:54 AM