SQL >> Base de Datos >  >> RDS >> Sqlserver

Entity Framework Code First con sinónimos de SQL Server

Para usar tablas compartidas y locales en un mismo contexto, crea sinónimos en la base local.

CREATE SYNONYM [dbo].[StudentCources]
FOR [SharedServer].[SharedDB].[dbo].[StudentCources];

Luego mapea la relación en OnModelCreating.

modelBuilder.Entity(Of Student)
    .HasMany(Function(e) e.Courses)
    .WithMany(Function(e) e.Students)
    .Map(Sub(e)
        e.MapLeftKey("studentID")
        e.MapRightKey("courseID")
        e.ToTable("StudentCources", "dbo")
    End Sub)

Para uno a muchos, basta mapear la tabla o sinónimo correspondiente.