Having a strange issue with SQL Server Synonyms and EF6.
A SQL Server database is set up with tables containing various non-dbo schemas (i.e. AZFSTSA and FMS). I would like the ASP.NET MVC application to be agnostic of the schema name, so each table has a synonym using dbo. The application should therefore be able to access all tables via dbo.
For example:
Table is actually named AZFSTSA.DataAudits and the corresponding synonym is
CREATE SYNONYM [dbo].[DataAudits] FOR [AZFSTSA].[DataAudits]
There are several entity classes and a datacontext class.
For example:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Application.Entity.Models
{
[Table("dbo.DataAudits")]
public partial class DataAudit
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[Required]
[Column(Order = 1)]
public DateTime Completed { get; set; }
[Required]
[Column(Order = 2)]
[StringLength(100)]
public string Process { get; set; }
[Required]
[Column(Order = 3)]
[StringLength(8000)]
public string Result { get; set; }
}
}
and
public class DataContext : DbContext
{
public DataContext()
: base("name=DataContext")
{
}
public virtual DbSet<DataAudit> DataAudit { get; set; }
public virtual DbSet<AGCYFL> AGCYFL { get; set; }
}
There is no EF migration code in the application. All entity classes contain the same table attribute referencing dbo
When I run the application, the first instance of data access produces the following error:
System.Data.SqlClient.SqlException: ‘There is already an object named ‘CbAgencies’ in the database.’
CbAgencies happens to be the first class in the EF Entities folder.
I tried to change the table name so it differs from the synonym alias (new table name = Audits, synonym alias is dbo.DataAudits). However the same error was noticed.
If I modify the Table attribute only in the first entity class (CbAgencies) to include the actual schema (instead of dbo), the application runs fine. However as I mentioned earlier, I do not want to include the actual schema in case the schema changes.
How can I set up my application such that I do not need to use the actual schema name?
3