在连接表asp.net MVC 4 Entity framework 6中插入记录。

时间:2022-09-18 08:41:43

I am using Entity framework 6 in my asp.net MVC 4 application. I have two tables with many to many reationship profiles and profilepositions. I created a junction table with ids of both. I am using code first approach. How can I insert record in this table ? Do I need to create virtual properties in both tables for junction table or what.

我正在我的asp.net MVC 4应用中使用实体框架6。我有两张表格,上面有很多的交友资料和个人资料。我创建了一个包含两个id的连接表。我正在使用代码优先方法。如何在该表中插入记录?我需要在两个表中为连接表创建虚拟属性吗?

 public class Profile
    {
       public Guid ProfileId { get; set; }
       // other properties
    }

public class Account
    {
        public Guid AccountId { get; set; }
        // other properties

    }

// junction table
 public class AccountConnection
    {
       public int AccountID { get; set; }
       public int ProfileID { get; set; }
    }

Please suggest

请建议

1 个解决方案

#1


2  

you would build the tables like this:

你可以这样构建表格:

public class Profile
{
   public Guid ProfileId { get; set; }
   public virtual ICollection<Account> Accounts { get; set; }

   // other properties
}

Public class Account
{
    public Guid AccountId { get; set; }
    public virtual ICollection<Profile> Profiles { get; set; }
    // other properties

}

To add records to either tables you just use Profile.Accounts.Add( 'Account Model' ) and Account.Profiles.Add( 'Profile Model' )

要向任何一个表添加记录,只需使用Profile.Accounts。添加('Account Model')和Account. profiles。Add(概要模型)

The junction table is created automatically

连接表是自动创建的。

#1


2  

you would build the tables like this:

你可以这样构建表格:

public class Profile
{
   public Guid ProfileId { get; set; }
   public virtual ICollection<Account> Accounts { get; set; }

   // other properties
}

Public class Account
{
    public Guid AccountId { get; set; }
    public virtual ICollection<Profile> Profiles { get; set; }
    // other properties

}

To add records to either tables you just use Profile.Accounts.Add( 'Account Model' ) and Account.Profiles.Add( 'Profile Model' )

要向任何一个表添加记录,只需使用Profile.Accounts。添加('Account Model')和Account. profiles。Add(概要模型)

The junction table is created automatically

连接表是自动创建的。