I have different contexts in two different domains and each domain contains an entity "country". Both entities should use the same table "country". One entity contains xzy properties and the other contains xz properties. The contexts do not know each other. The issue is that I am getting the error: "There is already an object named 'Country' in the database." What is the best approach in this case, please? Thank you in advance.
-
you can create two table with the same name in different schema. – hassan.ef Apr 24 '19 at 14:56
-
1Possible duplicate of [Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?](https://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea) – Paulo Campez Apr 24 '19 at 14:56
3 Answers
Normally an entity would belong only to one context/domain. if you need this entity in another domain, simply use the context it's defined in. there is nothing wrong with using multiple contexts in a transaction. What you're trying to do is conceptually and technically not recommended even though it is possible.

- 1,883
- 4
- 17
- 31
You need to change the name of the generated table: How to Specify Entity Framework Core Table Mapping?
Either use:
[Table("CountriesCustomTableName")]
public class Country{ x, y, z }
or override OnModelCreating like so:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Country>(entity => {
entity.ToTable("CountriesCustomTableName");
});
}
Remember to fully qualify your class names or you'll get confused or have ambiguous compilation errors
Edit
If you're trying to map 2 different entity models to the same table it sounds like you're pattern or design could be improved upon.
I would suggest you either need to use a full domain model, but only half populate it when you're inputting your 'half domain model'
public class Country {
public string X { get; set; }
public string Y { get; set; }
public string Z { get; set; }
}
var country1 = new Country() { X = "A", Y = "B", Z = "C" };
var country2 = new Country() { X = "A", Z = "C" };
context.Countries.Add(country1);
context.Countries.Add(country2);
Or (more likely) This is some form of code smell and you need to abstract out the optional values into another table e.g:
public class Country {
public string X { get; set; }
public string Z { get; set; }
}
public class CountryYInfo {
public string Y { get; set; }
public Country Country { get; set; }
public int CountryId { get; set; }
}
var country1 = new Country() { X = "A", Z = "C" };
var countryInfo = new CountryYInfo { Y = "B", Country = country1 };
var country2 = new Country() { X = "A", Z = "C" };
context.Countries.Add(country1);
context.Countries.Add(country2);

- 2,170
- 6
- 29
- 61
-
Thanks for your reply. I think I did not make it clear, but the point is that both entities are using the same table. – Lex Apr 25 '19 at 07:09
-
@Lex What you're trying to do (Map 2 different models to the same table is impossible and bad practice) I've edited my answer to show possible solutions to your problem – Smithy Apr 25 '19 at 10:24
If you want to share the country data between two domains, then "Country" is logically considered another domain. Reference the country record by its id instead of interdomain refrencing which is an awful practice in DDD.

- 4,268
- 1
- 15
- 35