Create code first, many to many, with additional fields in association table

前端 未结 6 1674
星月不相逢
星月不相逢 2020-11-21 10:21

I have this scenario:

public class Member
{
    public int MemberID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get         


        
6条回答
  •  不思量自难忘°
    2020-11-21 10:26

    The code provided by this answer is right, but incomplete, I've tested it. There are missing properties in "UserEmail" class:

        public UserTest UserTest { get; set; }
        public EmailTest EmailTest { get; set; }
    

    I post the code I've tested if someone is interested. Regards

    using System.Data.Entity;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Web;
    
    #region example2
    public class UserTest
    {
        public int UserTestID { get; set; }
        public string UserTestname { get; set; }
        public string Password { get; set; }
    
        public ICollection UserTestEmailTests { get; set; }
    
        public static void DoSomeTest(ApplicationDbContext context)
        {
    
            for (int i = 0; i < 5; i++)
            {
                var user = context.UserTest.Add(new UserTest() { UserTestname = "Test" + i });
                var address = context.EmailTest.Add(new EmailTest() { Address = "address@" + i });
            }
            context.SaveChanges();
    
            foreach (var user in context.UserTest.Include(t => t.UserTestEmailTests))
            {
                foreach (var address in context.EmailTest)
                {
                    user.UserTestEmailTests.Add(new UserTestEmailTest() { UserTest = user, EmailTest = address, n1 = user.UserTestID, n2 = address.EmailTestID });
                }
            }
            context.SaveChanges();
        }
    }
    
    public class EmailTest
    {
        public int EmailTestID { get; set; }
        public string Address { get; set; }
    
        public ICollection UserTestEmailTests { get; set; }
    }
    
    public class UserTestEmailTest
    {
        public int UserTestID { get; set; }
        public UserTest UserTest { get; set; }
        public int EmailTestID { get; set; }
        public EmailTest EmailTest { get; set; }
        public int n1 { get; set; }
        public int n2 { get; set; }
    
    
        //Call this code from ApplicationDbContext.ConfigureMapping
        //and add this lines as well:
        //public System.Data.Entity.DbSet UserTest { get; set; }
        //public System.Data.Entity.DbSet EmailTest { get; set; }
        internal static void RelateFluent(System.Data.Entity.DbModelBuilder builder)
        {
            // Primary keys
            builder.Entity().HasKey(q => q.UserTestID);
            builder.Entity().HasKey(q => q.EmailTestID);
    
            builder.Entity().HasKey(q =>
                new
                {
                    q.UserTestID,
                    q.EmailTestID
                });
    
            // Relationships
            builder.Entity()
                .HasRequired(t => t.EmailTest)
                .WithMany(t => t.UserTestEmailTests)
                .HasForeignKey(t => t.EmailTestID);
    
            builder.Entity()
                .HasRequired(t => t.UserTest)
                .WithMany(t => t.UserTestEmailTests)
                .HasForeignKey(t => t.UserTestID);
        }
    }
    #endregion
    

提交回复
热议问题