none
(Code First) 1:n assocition , twice same table RRS feed

  • Question

  • hi~ here my code

    now i making that (silverlight 5 + SQL Server Compact 4.0 + WCF RIA Service Support EF4.1) used public board programming

    i want, MST_MEMBER -> BOARD_LIST assocation twice how can i do??
    1 assocation : RegMember
    2 assocation : UptMember

    thank.

     

    using System.ComponentModel.DataAnnotations;
    using System.Collections.ObjectModel;
     
    namespace SL5_BOARD.Web.Model
    {
        public class MST_MEMBER
        {
            [Key]
            public int MST_MEMBER_IDX { get; set; }
            public string EMAIL { get; set; }
            public string PASSWORD { get; set; }
            public string MEMBER_NAME { get; set; }
            public string NICK_NAME { get; set; }
            public bool EMAIL_RECEIVE { get; set; }
            public string DESCRIPTION { get; set; }
            public DateTime LAST_LOGIN_DT { get; set; }
            public string JOB { get; set; }
            public string HOBBY { get; set; }
            public string INTEREST { get; set; }
            public int MEMBER_SCORE { get; set; }
            public string MEMBER_GRADE { get; set; }
            public string ETC { get; set; }
            public int STATUS { get; set; }
            public DateTime REG_DT { get; set; }
            public virtual MST_MEMBER RegMember { get; set; }
            public int REG_IDX { get; set; }
            public virtual MST_MEMBER UptMember { get; set; }
            public DateTime? UPT_DT { get; set; }
            public int? UPT_IDX { get; set; }
     
            public virtual ObservableCollection<BOARD_LIST> BoardLists { get; set; }
            public virtual ObservableCollection<BOARD_LIST> BoardList { get; set; }
     
            public MST_MEMBER()
            {
                BoardLists = new ObservableCollection<BOARD_LIST>();
                BoardList = new ObservableCollection<BOARD_LIST>();
            }
        }
    }

     

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Collections.ObjectModel;

    namespace SL5_BOARD.Web.Model
    {
        public class BOARD_LIST
        {
            [Key]
            public int BOARD_LIST_IDX { get; set; }
            public string LIST_TITLE { get; set; }
            public string LIST_CONTENT { get; set; }
            public bool LIST_ATTACH_YN { get; set; }
            public int LIST_SEQ { get; set; }
            public int LIST_LEVEL { get; set; }
            public int LIST_VIEW_COUNT { get; set; }
            public string LIST_WRITER_NAME { get; set; }
            [DataType(DataType.Password)]
            public string LIST_PASSWORD { get; set; }
            public string LIST_KIND { get; set; }
            public int LIST_COMPLETE_GB { get; set; }
            public bool LIST_OPEN_YN { get; set; }
            public int LIST_GOOD_COUNT { get; set; }
            public int LIST_PUBLIC_GB { get; set; }

            public virtual MST_MEMBER RegMember { get; set; }
            public int REG_IDX { get; set; }
            public virtual MST_MEMBER UptMember { get; set; }
            public DateTime? UPT_DT { get; set; }
            public int? UPT_IDX { get; set; }

            public virtual BOARD_MAIN BoardMain { get; set; }
            public int BOARD_MAIN_IDX { get; set; }

            public virtual ObservableCollection<BOARD_REPLY> BoardReplys { get; set; }
            public virtual ObservableCollection<BOARD_ATTACH> BoardAttachs { get; set; }

            public BOARD_LIST()
            {
                LIST_ATTACH_YN = false;
                LIST_SEQ = 0;
                LIST_LEVEL = 1;
                LIST_VIEW_COUNT = 0;
                LIST_COMPLETE_GB = 0;
                LIST_OPEN_YN = true;
                LIST_GOOD_COUNT = 0;
                LIST_PUBLIC_GB = 0;

                BoardReplys = new ObservableCollection<BOARD_REPLY>();
                BoardAttachs = new ObservableCollection<BOARD_ATTACH>();
            }
        }
    }

    Tuesday, July 12, 2011 3:23 AM

Answers

  • but, i don't know that make two association link.

    how can i do that?

    Hello,

    Thank you for posting.

    From your description, it looks that you should create a map file between two classes which have relationship each other. For example, I have a Parent table and a Child table, they were designed was similar like your above code snippets. But you should add other mapping files in your project like the following.

    public ParentMap()
    {
    	// Primary Key 
    	this.HasKey(t => t.id);
    
    	// Properties
    	this.Property(t => t.ParentName).IsFixedLength().HasMaxLength(10);
    				
    	// Table & Column Mappings
    	this.ToTable("Parent");
    	this.Property(t => t.Price).HasColumnName("Price");
    
    	// Relationships RequiredNavigationPropertyConfiguration
    	this.HasRequired(t => t.Parent2).WithOptional(t => t.Parent1);
    				
    }
    public ChildMap()
    {
    	// Primary Key
    	this.HasKey(t => t.id);
    
    	// Properties
    	this.Property(t => t.name).IsRequired().IsFixedLength().HasMaxLength(10);
    				
    	// Table & Column Mappings
    	this.ToTable("Children");
    	this.Property(t => t.id).HasColumnName("id");
    
    	// Relationships ForeignKeyNavigationPropertyConfiguration
    	this.HasRequired(t => t.Parent).WithOptional(t => t.Child);
    				
    }
    

    Please check this blog for more detailed information. http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4codefirstwalkthrough.aspx

    In addition, if your database has already been designed, I suggest you can download EF Power Tools CTP1 Released. Please check it here. http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

    Best Regards,


    Larcolais Gong[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.

    Wednesday, July 13, 2011 7:57 AM

All replies

  • What is the issue with the current code? To get a reference twice (or more) you add that type multiple times. You have already done it. However you use the MST_MEMBER type in MST_MEMBER for RegMember and UptMember and will cause reference to same table, nothing wrong with it if that is what you want.

    Here is small example of multiple references to child table. Employee with multiple addresses, home and office.

        public class Address
        {
            [Key]
            public Guid Id { getset; }
            public string Name { getset; }
        }
     
        public class Employee
        {
            [Key]
            public Guid Id { getset; }
            public string Name { getset; }
            public Address HomeAddress { getset; }
            public Address OfficeAddress { getset; }
        }
     
        public class CodeFirstDb : DbContext
        {
            public DbSet<Address> Addresses { getset; }
        public DbSet<Employee> Employees { get; set; }
        }

    How to populate the database.

     Employee em = new Employee();
     em.Id = Guid.NewGuid();
     em.Name = "Andy";
     em.HomeAddress = new Address();
     em.HomeAddress.Id = Guid.NewGuid();
     em.HomeAddress.Name = "Home";
     em.OfficeAddress = new Address();
     em.OfficeAddress.Id = Guid.NewGuid();
     em.OfficeAddress.Name = "Office";
     
     db.Employees.Add(em);
     
     db.SaveChanges();
    Tuesday, July 12, 2011 7:30 AM
  • hi Andreas

    thank for your reply.

     

    ok . i try your code. but, raise error

    Error 1 Unable to retrieve association information for association 'SL5_BOARD.Web.Model.BOARD_LIST_RegMember'. Only models that include foreign key information are supported. See Entity Framework documentation for details on creating models that include foreign key information. SL5_BOARD

    i think so assocition is two tables each other reference.

    sample

     

    BOARD_MAIN : BOAR_LISt (1:N)

     

    BOARD_MAIN

         public class BOARD_MAIN
        {
            [Key]
            public int BOARD_MAIN_IDX { get; set; }
            public string BOARD_NAME { get; set; }
            public string BOARD_TYPE { get; set; }
            public bool MEMBER_CHECK { get; set; }
            public string BOARD_DESC { get; set; }
            public string BOARD_STATE { get; set; }
            public DateTime REG_DT { get; set; }
            public int REG_IDX { get; set; }
            public DateTime? UPT_DT { get; set; }
            public int? UPT_IDX { get; set; }

            public virtual ObservableCollection<BOARD_LIST> BoardLists { get; set; }

            public BOARD_MAIN()
            {
                BOARD_NAME = "New Board";
                MEMBER_CHECK = false;
                this.BoardLists = new ObservableCollection<BOARD_LIST>();
            }
        }

     

    BOARD_LIST

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Collections.ObjectModel;

    namespace SL5_BOARD.Web.Model
    {
        public class BOARD_LIST : ICloneable
        {
            [Key]
            public int BOARD_LIST_IDX { get; set; }
            public string LIST_TITLE { get; set; }
            public string LIST_CONTENT { get; set; }
            public bool LIST_ATTACH_YN { get; set; }
            public int LIST_SEQ { get; set; }
            public int LIST_LEVEL { get; set; }
            public int LIST_VIEW_COUNT { get; set; }
            public string LIST_WRITER_NAME { get; set; }
            [DataType(DataType.Password)]
            public string LIST_PASSWORD { get; set; }
            public string LIST_KIND { get; set; }
            public int LIST_COMPLETE_GB { get; set; }
            public bool LIST_OPEN_YN { get; set; }
            public int LIST_GOOD_COUNT { get; set; }
            public int LIST_PUBLIC_GB { get; set; }

            public virtual MST_MEMBER RegMember { get; set; }
            public DateTime REG_DT { get; set; }
            public int REG_IDX { get; set; }
            public virtual MST_MEMBER UptMember { get; set; }
            public DateTime? UPT_DT { get; set; }
            public int? UPT_IDX { get; set; }

            public virtual BOARD_MAIN BoardMain { get; set; }
            public int BOARD_MAIN_IDX { get; set; }

            public virtual ObservableCollection<BOARD_REPLY> BoardReplys { get; set; }
            public virtual ObservableCollection<BOARD_ATTACH> BoardAttachs { get; set; }

            public BOARD_LIST()
            {
                LIST_ATTACH_YN = false;
                LIST_SEQ = 0;
                LIST_LEVEL = 1;
                LIST_VIEW_COUNT = 0;
                LIST_COMPLETE_GB = 0;
                LIST_OPEN_YN = true;
                LIST_GOOD_COUNT = 0;
                LIST_PUBLIC_GB = 0;

                BoardReplys = new ObservableCollection<BOARD_REPLY>();
                BoardAttachs = new ObservableCollection<BOARD_ATTACH>();
            }

            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }
    }


    but, i don't know that make two association link.

    how can i do that?

     

    ps. EntityFramework.4.1.10331.0 version used

    Tuesday, July 12, 2011 9:04 AM
  • Maybe I am misunderstanding what you want to do. It is a bit difficult to get a good overview from your code snippets. Why not write more as C# code than SQL?

    With the least amount of code, how are you know trying to define the associations?

    Tuesday, July 12, 2011 11:39 AM
  • but, i don't know that make two association link.

    how can i do that?

    Hello,

    Thank you for posting.

    From your description, it looks that you should create a map file between two classes which have relationship each other. For example, I have a Parent table and a Child table, they were designed was similar like your above code snippets. But you should add other mapping files in your project like the following.

    public ParentMap()
    {
    	// Primary Key 
    	this.HasKey(t => t.id);
    
    	// Properties
    	this.Property(t => t.ParentName).IsFixedLength().HasMaxLength(10);
    				
    	// Table & Column Mappings
    	this.ToTable("Parent");
    	this.Property(t => t.Price).HasColumnName("Price");
    
    	// Relationships RequiredNavigationPropertyConfiguration
    	this.HasRequired(t => t.Parent2).WithOptional(t => t.Parent1);
    				
    }
    public ChildMap()
    {
    	// Primary Key
    	this.HasKey(t => t.id);
    
    	// Properties
    	this.Property(t => t.name).IsRequired().IsFixedLength().HasMaxLength(10);
    				
    	// Table & Column Mappings
    	this.ToTable("Children");
    	this.Property(t => t.id).HasColumnName("id");
    
    	// Relationships ForeignKeyNavigationPropertyConfiguration
    	this.HasRequired(t => t.Parent).WithOptional(t => t.Child);
    				
    }
    

    Please check this blog for more detailed information. http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4codefirstwalkthrough.aspx

    In addition, if your database has already been designed, I suggest you can download EF Power Tools CTP1 Released. Please check it here. http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

    Best Regards,


    Larcolais Gong[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.

    Wednesday, July 13, 2011 7:57 AM
  • thank you Larcolais

    i will try make map..

    so. here is my public board full source..(developping)

    http://www.kakisoft.com/download/sl_board_fullsource.zip

     

    @Andreas have a good day, thank you too

     

    Wednesday, July 13, 2011 11:34 AM