简体   繁体   English

添加一列以联接表EF6

[英]Add a column to join table EF6

I'm working with EF6 code first with database migrations and I'm a beginner. 我首先使用EF6代码进行数据库迁移,并且是一个初学者。 I'm building a web api. 我正在构建一个Web API。 In my model plan I have a User (Identity Framework) and a Location class with a many to many relationship. 在我的模型计划中,我有一个用户(身份框架)和一个具有多对多关系的Location类。 EF6 automatic created a table LocationUser but I want to extend this table with some extra columns. EF6自动创建了一个表LocationUser,但是我想用一些额外的列来扩展该表。 But what is the best way to do that. 但是什么是最好的方法。

Of course I've searched for some solutions but I'm not sure what is the best way to go. 当然,我已经搜索了一些解决方案,但是我不确定最好的方法是什么。

I can edit the last migration Up method and add the columns I want but I'm not sure if this is the best way. 我可以编辑最后一个Up方法,并添加所需的列,但是我不确定这是否是最佳方法。 In my opinion creating a new model class manually is also a possibility. 我认为手动创建新的模型类也是可行的。

Could anyone tell me what is the best way and elaborate why? 谁能告诉我什么是最好的方法,并详细说明为什么?

User.cs User.cs

public class User : IdentityUser
{
    public virtual ICollection<Location> Locations { get; set; }
    // Other code for Identity Framework
}

Location.cs Location.cs

public class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public double Lat { get; set; }
    public double Lng { get; set; }

    public virtual ICollection<User> Users{ get; set; }
}

The best way in my opinion is to use fluet api in its context follows a tutorial for better understanding: 我认为最好的方法是在教程中使用fluet api,以便更好地理解:

Sample: 样品:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<User>()
                .HasMany<Location>(u => u.Locations)
                .WithMany(l => l.Students)
                .Map(ul =>
                        {
                            cs.MapLeftKey("UserId");
                            cs.MapRightKey("LocationId");
                            cs.ToTable("UserLocation");
                        });

}

Tutorial: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx 教程: http//www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

For updates to the model I always create new migrations so as not to compromise the actual history of actions. 为了更新模型,我总是创建新的迁移,以免影响实际的操作历史。

You can create your own UserLocation class like below. 您可以如下创建自己的UserLocation类。

public class UserLocation
{
    public int Id { get; set; }
    public Location Location { get; set; }
    public User User { get; set; }
    //Extra fields you want can go here
}

Then in your User and Location classes you would change the link to use your UserLocation class. 然后,在您的UserLocation类中,您将更改链接以使用UserLocation类。

Example: 例:

public class User : IdentityUser
{
    public virtual ICollection<UserLocation> Locations { get; set; }
    // Other code for Identity Framework
}

Can also add EntityTypeConfigurations to your classes to enforce the many-many, below would be the one for User 也可以将EntityTypeConfigurations添加到您的类中以强制执行很多,以下将是User的一种

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        HasKey(x => x.Id);
        HasMany(x => x.Locations);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM