Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Entity Framework 6 - How to handle multiple schemas that have same table names

I'm using C# Entity Framework 6 and I have a database with multiple schemas, in those schemas I have tables with the same names. For example:

  1. Rates.Hotel
  2. Availability.Hotel

I have a single project for all of my database objects with this structure:

Project Name = Storm.API.Data
In this project I have folders with the schema names: "Availability", "Rates".
In each folder I have the proper 'edmx' file.

When I run my project I'm getting this error:

Schema specified is not valid. Errors: 

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM 
type 'Hotel'. Previously found CLR type 'Storm.API.Data.Rates.Hotel', 
newly found CLR type 'Storm.API.Data.Availability.Hotel'.

The only solution I came up with is to separate the schemas to different project, but since I have more than 2 schemas I don't want to create so many project just for that.

Is there another solution that will enable me to have all schemas under the same project without this error ?

like image 459
Liran Friedman Avatar asked Jan 21 '26 16:01

Liran Friedman


1 Answers

You can:

  1. Give each of them a different class name in your code
  2. Use 2 different DbContext classes, with a different namespace for the entities. Whether you put this in another project is up to you, but not required.
  3. Not sure if this will work, but worth a shot: Make a namespace for every schema and have everything in 1 DbContext.
like image 86
H. Lowette Avatar answered Jan 24 '26 05:01

H. Lowette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!