locked
EF code-first to model a List<string> RRS feed

  • Question

  • I have a single object like this:

    class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<string> Nicknames { get; set; }
    }

    I'd like to use DbModelBuilder / EntityTypeConfiguration to map it to 2 tables so 2 persons, each with 2 nicknames can be persisted like this:

    // Person table
    Id    Name
    ----------
    1    Smith
    2    Doe

    // Nickname table
    Id    Nickname
    ------------
    1    AAA
    1    BBB
    2    CCC
    2    DDD

    Please advise.

    Thanks.

    Wednesday, September 14, 2011 2:13 PM

Answers

  • Hi gl20061026,

    Welcome!

    According to your description, it seems there is no way to use Fluent API and DataAnnotations to achieve your goal.

    There is a foreign relationship between Person table an NickName table, so you can add some code to do it:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace Liststring
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var context= new MyContext())
                {
                    context.Database.CreateIfNotExists();
                }
            }
        }
        public class Person
        {
            public int Id { get; set; }
            public string  Name { get; set; }
            public List<NickName> Nicknames { get; set; }
        }
        public class NickName
        {
            public int Id { get; set; }
            public string Nick_Name { get; set; }
            public Person Person { get; set; }
        }
        public class MyContext:DbContext
        {
            public DbSet<Person> Persons { get; set; }
            public DbSet<NickName> NickNames { get; set; }
        }
    }
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by gl20061026 Monday, September 19, 2011 4:13 PM
    Thursday, September 15, 2011 7:18 AM

All replies

  • Hi gl20061026,

    Welcome!

    According to your description, it seems there is no way to use Fluent API and DataAnnotations to achieve your goal.

    There is a foreign relationship between Person table an NickName table, so you can add some code to do it:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace Liststring
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var context= new MyContext())
                {
                    context.Database.CreateIfNotExists();
                }
            }
        }
        public class Person
        {
            public int Id { get; set; }
            public string  Name { get; set; }
            public List<NickName> Nicknames { get; set; }
        }
        public class NickName
        {
            public int Id { get; set; }
            public string Nick_Name { get; set; }
            public Person Person { get; set; }
        }
        public class MyContext:DbContext
        {
            public DbSet<Person> Persons { get; set; }
            public DbSet<NickName> NickNames { get; set; }
        }
    }
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by gl20061026 Monday, September 19, 2011 4:13 PM
    Thursday, September 15, 2011 7:18 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 19, 2011 7:44 AM
  • Thanks Alan,

    That's pretty much what I did but thanks for verifying that what I did is correct.

     

     

    Monday, September 19, 2011 4:16 PM