3

I have got model with M:N relation like this:

[Table("Messages", Schema = "public")]
public class Message
{

    public int Id  { get; set; }
    public virtual IList<Phone> Phones{ get; set; }

}

[Table("Phones", Schema = "public")]
public class Phone
{

    public int Id  { get; set; }
    public virtual IList<Message> Messages{ get; set; }

}

So EF generates middle table for me... But default schema of table is not public (it is what i need), but still dbo. And gives me error: schema "dbo" does not exist.

How can I change table schema of MessagePhone table, without creating MessagePhone model class?

Alamakanambra
  • 5,845
  • 3
  • 36
  • 43

2 Answers2

2

I think it is doable. You will have to override OnModelCreating of your DbContext class and configure with fluent API:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     modelBuilder.Entity<Message>().HasMany<Phone>(m => m.Phones).WithMany(p => p.Messages).Map
           (
            x =>
               {
                  x.ToTable("MessagePhone", "public");
               }
           );
 }

You will have to test this, not sure i got all of the syntax right.. Don't have VS here to try it.

Jurica Smircic
  • 6,117
  • 2
  • 22
  • 27
0

Have you tried it like this?

Basically you create the middle table yourself, and have many-to-one relationships to it. That way, you can specify anything you like about it. The downside is of course, that you cannot just handle the relation like a collection.

Community
  • 1
  • 1
Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45