I have a class Tenant, which links to a User via a TenantUser table in the database.
This relationship is many => many, though I actually only want to expose the mapping one way.
Tenant has a property
virtual ICollection<User> Users { get; set; }
The User class has no property mapping back.
Is it possible using properties or the fluent API to map the Users property of Tenant so that it collects the Users for a particular Tenant via the TenantUser table.
Is that possible, or does the User class need a Tenant collection in order to specify any kind of mapping?
I will also (optionally) have a
DbSet<TenantUser>
in the context to manage the intermediary table, but for convenience, I want the property accessible on the Tenant class.
Thanks.
You can define a many-to-many mapping in Fluent API, also if only one collection is exposed in the model classes:
You can’t define this relationship with data annotations.
Be aware that you don’t have a
TenantUserentity in a many-to-many relationship in Entity Framework. Therefore you can’t create aDbSet<TenantUser>in your context to manage the link table. EF manages this table internally when you add or delete relationships by adding and removing elements to/from theTenant.Userscollection.