locked
Code Only - one-to-one mappin RRS feed

  • Question

  • Anyone know how to map one-to-one, for example

     

    class Person

    {

          int person_id;

          Address address;

    }

     

    class Address

    {

        int address_id;

        string name;

    }

     

    It is seem that EF needs yesrs to be simple and efficient !!!

     

    Thanks

    • Moved by Jonathan Aneja -- MSFTModerator Wednesday, April 21, 2010 4:43 AM Code First is a pre-release technology (From:ADO.NET Entity Framework and LINQ to Entities)
    Wednesday, April 14, 2010 7:11 AM

Answers

  • Hello,

    If you want to set a FK from address_id to the person_id to make it as an one-to-one relationship, yes, Code Only can help us to achieve it.  Please note that currenctly, this feature is named Code First which is formly named as Code Only.  The latest version of this feature is in EF4 CTP3, http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&displayLang=en.   For VS2010, we can install the CTP3 without problems.   All the Code First classes are in the assembly Microsoft.Data.Entity.CTP.  

    Here are some sample codes:
    =============================================================================
        public class ContextContainer : ObjectContext
        {
            public ContextContainer(EntityConnection connection)
                : base(connection)
            {
                DefaultContainerName = "ContextContainer";
            }

            public IObjectSet<Person> People
            {
                get { return base.CreateObjectSet<Person>(); }
            }

            public IObjectSet<Address> Addresses
            {
                get { return base.CreateObjectSet<Address>(); }
            }
        }

        public class Person
        {
            public int ID { get; set; }
            public string Name { get; set; }

            public Address Address { get; set; }
        }

        public class Address
        {
            public int ID { get; set; }
            public string Value { get; set; }

            public Person Person { get; set; }
        }

        public class PersonConfiguration : EntityConfiguration<Person>
        {
            public PersonConfiguration()
            {
                HasKey(p => p.ID);
            }
        }

        public class AddressConfiguration : EntityConfiguration<Address>
        {
            public AddressConfiguration()
            {
                HasKey(a => a.ID);
                Relationship(a => a.Person).IsRequired()
                    .FromProperty(p => p.Address)
                    .HasConstraint((a, p) => a.ID == p.ID);
            }
        }
    =============================================================================
    To create the .edmx:

                string DB_CONN = @"Data Source=.\SQLEXPRESS;" +
                "Initial Catalog=CodeOnlyDB;Integrated Security=SSPI;";

                ContextBuilder<ContextContainer> builder =
                    new ContextBuilder<ContextContainer>();
                SqlConnection conn = new SqlConnection(DB_CONN);
                builder.Configurations.Add(new PersonConfiguration());
                builder.Configurations.Add(new AddressConfiguration());

                // Create a XmlWriter object
                XmlWriter writer = new XmlTextWriter("CodeOnly.edmx",
                    Encoding.UTF8);
                try
                {
                    // Write the .edmx file by the ContextBuilder
                    Console.Write("Creating the .edmx file...");
                    builder.WriteEdmx(conn, writer);
                    Console.WriteLine("Successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed!");
                    Console.WriteLine(ex.ToString());
                }
    =============================================================================

    For more sample codes, please see the CSEFCodeOnly of All-In-One Code Framework, which is an open-source project owned by MSDN forum support team.  


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 20, 2010 4:17 PM

All replies

  • Hello,

    If you want to set a FK from address_id to the person_id to make it as an one-to-one relationship, yes, Code Only can help us to achieve it.  Please note that currenctly, this feature is named Code First which is formly named as Code Only.  The latest version of this feature is in EF4 CTP3, http://www.microsoft.com/downloads/details.aspx?FamilyID=af18e652-9ea7-478b-8b41-8424b94e3f58&displayLang=en.   For VS2010, we can install the CTP3 without problems.   All the Code First classes are in the assembly Microsoft.Data.Entity.CTP.  

    Here are some sample codes:
    =============================================================================
        public class ContextContainer : ObjectContext
        {
            public ContextContainer(EntityConnection connection)
                : base(connection)
            {
                DefaultContainerName = "ContextContainer";
            }

            public IObjectSet<Person> People
            {
                get { return base.CreateObjectSet<Person>(); }
            }

            public IObjectSet<Address> Addresses
            {
                get { return base.CreateObjectSet<Address>(); }
            }
        }

        public class Person
        {
            public int ID { get; set; }
            public string Name { get; set; }

            public Address Address { get; set; }
        }

        public class Address
        {
            public int ID { get; set; }
            public string Value { get; set; }

            public Person Person { get; set; }
        }

        public class PersonConfiguration : EntityConfiguration<Person>
        {
            public PersonConfiguration()
            {
                HasKey(p => p.ID);
            }
        }

        public class AddressConfiguration : EntityConfiguration<Address>
        {
            public AddressConfiguration()
            {
                HasKey(a => a.ID);
                Relationship(a => a.Person).IsRequired()
                    .FromProperty(p => p.Address)
                    .HasConstraint((a, p) => a.ID == p.ID);
            }
        }
    =============================================================================
    To create the .edmx:

                string DB_CONN = @"Data Source=.\SQLEXPRESS;" +
                "Initial Catalog=CodeOnlyDB;Integrated Security=SSPI;";

                ContextBuilder<ContextContainer> builder =
                    new ContextBuilder<ContextContainer>();
                SqlConnection conn = new SqlConnection(DB_CONN);
                builder.Configurations.Add(new PersonConfiguration());
                builder.Configurations.Add(new AddressConfiguration());

                // Create a XmlWriter object
                XmlWriter writer = new XmlTextWriter("CodeOnly.edmx",
                    Encoding.UTF8);
                try
                {
                    // Write the .edmx file by the ContextBuilder
                    Console.Write("Creating the .edmx file...");
                    builder.WriteEdmx(conn, writer);
                    Console.WriteLine("Successfully!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed!");
                    Console.WriteLine(ex.ToString());
                }
    =============================================================================

    For more sample codes, please see the CSEFCodeOnly of All-In-One Code Framework, which is an open-source project owned by MSDN forum support team.  


    Best Regards,
    Lingzhi Sun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 20, 2010 4:17 PM
  • The example is not the same:

     

    My Example: Person ---> Address

    Your Example: Person <---> Address

     

    How do I map my example with code only ?

     

    Thanks.

    Thursday, April 22, 2010 11:33 AM