简体   繁体   中英

Configuring Entity Framework Many to Many relationship between two tables?

I am trying to define a many to many relationship between two tables using code first on existing tables. The "State" table and class can not be modified so I am trying to find a way to make that relationship work in Entity Framework without modifying the table or class.

I have a table in SQL that is represented by the class:

 [Table("MySchema.Content")]
public class Content 
{ 
    public int Id { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
    public virtual ICollection<State> States { get; set; }
}

I have a second table representing a list of States. (This class and table can not be changed) The class is:

[Table("dbo.State")]
public class State
{
    public int StateID { get; set; }
    public string Name { get; set; }
    public string ShortName { get; set; }
}

I have manually created a join table called MySchema.Content_State that contains ContentID and StateID as the two columns.

In my data context class I would like to use ModelBuilder to create the relationship but have not been able to figure out how to get it to work.

In the Context I've tried something like this:

 modelBuilder.Entity<Content>()
           .HasMany<State>(a => a.States)
           .WithMany(a => a.Contents)
           .Map(a =>
           {
               a.ToTable("Content_State", "MySchema");
               a.MapLeftKey("ContentId");
               a.MapRightKey("StateID");
           });

The above won't compile, complaining about the WithMany line.

How can I configure this to work against my constructed join table?

your models

  [Table("MySchema.Content")]
public class Content
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
    public ICollection<ContentState> ContentStates { get; set; }

}

[Table("dbo.State")]
public class State
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ShortName { get; set; }
    public ICollection<ContentState> ContentStates { get; set; }

}

public class ContentState
{
    public int ContentId { get; set; }
    public int StateId { get; set; }
    public Content Content { get; set; }
    public State State { get; set; }
}

your mapping

      protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                 modelBuilder.Entity<ContentState>()
            .HasKey(bc => new { bc.StateId, bc.ContentId });


        modelBuilder.Entity<ContentState>()
            .HasOne(bc => bc.Content)
            .WithMany(b => b.ContentStates)
            .HasForeignKey(bc => bc.ContentId);


        modelBuilder.Entity<ContentState>()
            .HasOne(bc => bc.State)
            .WithMany(c => c.ContentStates)
            .HasForeignKey(bc => bc.StateId);
            }

In the Context I just changed the code to:

modelBuilder.Entity<Content>()
       .HasMany<State>(a => a.States)
       .WithMany()
       .Map(a =>
       {
           a.ToTable("Content_State", "MySchema");
           a.MapLeftKey("ContentId");
           a.MapRightKey("StateID");
       });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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